exec [-a NAME] [-cl] [COMMAND] [ARG...] [REDIRECTION...]
The exec
builtin command is used to
If only redirections are given, the redirections affect the current shell without executing any program.
Option | Description |
---|---|
-a NAME | Passes NAME as zeroth argument for the program to be executed |
-c | Execute the program with an empty (cleared) environment |
-l | Prepends a dash (- ) to the zeroth argument of the program to be executed, similar to what the login program does |
exec
returns failureexec
returns failure
myprog=/bin/ls echo "This is the wrapper script, it will exec $myprog" # do some vodoo here, probably change the arguments etc. # well, stuff a wrapper is there for exec "$myprog" "$@"
# open it exec 3< input.txt # for example: read one line from the file(-descriptor) read -u 3 LINE # or read LINE <&3 # finally, close it exec 3<&-
To redirect the whole stdout
and stderr
of the shell or shellscript to a file, you can use the exec
builtin command:
exec >/var/adm/my.log 2>&1 # script continues here...
exec
can't find the program to execute, the error code shall be 126-a NAME
option appeared in Bash 4.2-alphaexec
(like -c
, -l
, -a NAME
).