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 execute a shell script from the command line, just prefix the script’s name with ./
./myscript
You might be tempted to 'fix' this by adding '.' to your $PATH but this is widely considered to be bad practice for security reasons.
A better plan is putting all shell scripts into a folder and adding that one folder to your $PATH

This is a BASH shell builtin, to display your local syntax from the bash prompt type: help [e]xec

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.

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

Related macOS commands

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.
Run a bash script
su - Run a command with substitute user and group id.


 
Copyright © 1999-2024 SS64.com
Some rights reserved