exec

Execute the specified command in place of the current shell.

Syntax
      exec [-cl] [-a name] [command [arguments]]
Key
   -l   Place a dash at the beginning of the zeroth arg passed
        to command. This is what login does.

   -a   The shell passes name as the zeroth argument to the executed command.

   -c   Execute Command with an empty environment.

If command is specified, it replaces the shell. No new process is created. The arguments become the arguments to command.

If command cannot be executed for some reason, a non-interactive shell exits, unless the shell option execfail is enabled, in which case it returns failure. An interactive shell returns failure if the file cannot be executed.

If command is not specified, any redirections take effect in the current shell, and the return status is 0.
If there is a redirection error, the return status is 1.

To simply execute a shell script from the command line, just prefix the script's name with ./
./myscript
You may be tempted to fix this by adding '.' to your $PATH but this is widely considered to be a bad idea for security reasons.
A better plan is putting all shell scripts into a folder and adding that one folder to your $PATH

Examples

Find all .png files (across sub-folders) and copy them into a single folder:

find / -name "*.png" -exec cp {} /Users/khalid/backuppics \;

Find all .py files in the python folder and subfolders and delete them all with rm:

find ~/python / -name \*.py -exec rm {} +

In these examples, {} will be replaced with the current file name.

exec is a bash built in command.

"Power tends to corrupt, absolute power corrupts absolutely" ~ Lord Acton

Related:

crontab - Schedule a command to run at a later time
chroot - Run a command with a different root directory
if - Conditionally perform a command
nohup - Run a command immune to hangups
su - Run a command with substitute user and group id


© Copyright SS64.com 1999-2013
Some rights reserved