Not able to understand use exec command

Hi

i am in learning phase of unix.
i was going through exec in a unix book. below is the command

exec n>file
exec n>>file

however when i used the exec command like below , where ex is the file name

exec 2>>ex

and then do

 ls -lrt 

then again when i do the

ls -lrt 

to see the size of the file ex but what i see is it again appending the o/p of the

 ls -lrt 

to the file ex.

from this what i understand is after running the exec command the control is not coming out the exec command.

below is the sequence which happened to me .

 $ exec 1>>ex
$ ls -lrt
$ ls -lrt
$ ls -lrt

it is not printing the o/p on the screen when i run the ls -lrt command second time.

i am just puzzled.

regards,
scriptor

That is expected.

$ exec 1>>ex

You are redirecting (and appending) STDOUT to file ex - hence STDOUT is no longer "connected" to your terminal.

HI tokiwinter,

thx for your prompt response. however i am still not able to understand the working of exec command.
the below command simple overwrite the data

exec n>file

but this is not happening, instead it is keep on accepting the command and also appending the data also it seems that exec command is not leaving the control. please see below

$ exec 1>x1
$ ls -lrt
$ ls -lrt
$ 

this is what written in a book which i am referring. pasting the content below

By default, the shell provides you with three standard file descriptors for every command. With it, you can
also associate any file with file descriptors using the exec command.
Associating a file with a file description is useful when you need to redirect output or input to a file many
times but you don't want to repeat the filename several times.
To open a file for writing, use one of the following forms:
exec n>file
exec n>>file
Here n is an integer, and file is the name of the file you want to open for writing. The first form overwrites
the specified file if it exists. The second form appends to the specified file. For example, the following
command 
$ exec 4>fd4.out
associates the file fd4.out with the file descriptor 4.
To open a file for reading, you use the following form:
exec n<file
Here n is an integer, and file is the name of the file you want to open for reading

Hi,

This is all expected behaviour.

Also

$ exec 1>ex

Will overwrite, not append. If you just want to redirect the output of a single run of ls to a file, run

$ ls -ltr 1>ex

As STDOUT is the default, you don't even need the 1 here.

If you are trying to understand the functionality/purpose of the exec command I'd recommend reading your shell's man page.