Skip to main content
Engineering LibreTexts

13-C.8: Positional Parameters / exec Command / source Command

  • Page ID
    43197
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Command Line Arguments/Positional Parameters

    A command line argument is nothing but an argument sent to a program being called. A program can take any number of command line arguments. For example, type the following command:

    pbmac@pbmac-server $ ls grate_stories_of
    grate_stories_of: No such file or directory
    pbmac@pbmac-server $ ls great_stories_of
    great_stories_of
    

    There is no file or directory named grate_stories_of so the system issues an error message.

    ls is the name of an actual command and shell executed this command when you type command at shell prompt. The first word on the command line is:

    • ls - name of the command to be executed.
    • Everything else on command line is taken as arguments to this command. (ALTHOUGH many texts, this course included, make a distinction between options and arguments - with options specifying behavior of the command, whereas the actual arguments specify what to apply the options to.)

    Consider the following example:

    pbmac@pbmac-server $ tail +10 /path/to/file.txt
    
    • tail: Command name.
    • +10 /path/to/file.txt: The arguments.

    Examples

    Try the following command and note down its command line arguments:

    Command Command name Total number of arguments Argument name(s)
    ls ls 0 N/A
    ls /etc/resolv.conf ls 1 /etc/resolv.conf
    cp /etc/resolv.conf /tmp/test.txt cp 2 /etc/resolv.conf, and /tmp/test.txt
    sort -r -n /path/to/file sort 3 -r, -n, and /path/to/file
    date +"%d-%m-%Y" date 1 +"%d-%m-%Y"

    Command Line Parameters Different Names

    A command line parameter is also known as:

    • Command line options
    • Options
    • Positional parameters
    • Flag
    • Switches or switch
    • Command-line arguments

    Why Use Command Line Arguments

    • Telling the command/utility which option to use.
    • Informing the utility/command which file or group of files to process (reading/writing of files).

    Accessing Command-Line Arguments

    All command line parameters (positional parameters) are available via special shell variable $1, $2, $3,...,$9.

    The simple shell script called cmdargs.sh is shown in this example:

    #!/bin/bash
    echo "The script name : $0"
    echo "The value of the first argument to the script : $1"
    echo "The value of the second argument to the script : $2"
    echo "The value of the third argument to the script : $3"
    echo "The number of arguments passed to the script : $#"
    echo "The value of all command-line arguments (\$* version) : $*"
    echo "The value of all command-line arguments (\$@ version) : $@"
    

    When the script is run, the output looks like:

    pbmac@pbmac-server $ cmdargs.sh bmw ford toyota
    The script name : ./cmdargs.sh
    The value of the first argument to the script : bmw
    The value of the second argument to the script : ford
    The value of the third argument to the script : toyota
    The number of arguments passed to the script : 3
    The value of all command-line arguments ($* version) : bmw ford toyota
    The value of all command-line arguments ($@ version) : bmw ford toyota

    The following are additional examples; the results are in the table that follows.

    pbmac@pbmac-server $ ls /tmp
    pbmac@pbmac-server $ ./math 10 + 3
    pbmac@pbmac-server $ ~/scripts/addzone cyberciti.com
    pbmac@pbmac-server $ ~/scripts/adddomain cyberciti.biz '74.86.48.99' '2607:f0d0:1002:11::4' 
    pbmac@pbmac-server $ /etc/init.d/named reload
    pbmac@pbmac-server $ /usr/local/etc/rc.d/jail restart cyberciti.biz
    
    Shell script name ($0) Total number of arguments ($#) Actual Command line argument ($1,..,$9)
    ls 1 /tmp
    ./math 3 10, +, and 3
    ~/scripts/addzone 1 cyberciti.com
    ~/scripts/adddomain 3 cyberciti.biz, 74.86.48.99, and 2607:f0d0:1002:11::4
    /etc/init.d/named reload 1 reload
    /usr/local/etc/rc.d/jail 2 restart, and cyberciti.biz

    The Difference Between $@ and $*

    • $@ expanded as "$1" "$2" "$3" ... "$n"
    • $* expanded as "$1y$2y$3y...$n", where y is the value of $IFS (Input Field Seperator) variable i.e. "$*" is one long string and $IFS act as a separator or token delimiters.

    An example showing difference between these two variables:

    #!/bin/bash
    
    # Set Input FIeld Separator
    IFS=", "
    echo "* Displaying all pizza names using \$@"
    echo "$@"
    echo 
    
    # Output includes the field separator - the comma
    echo "* Displaying all pizza names using \$*"
    echo "$*"
    

    When the script runs the output is:

    pbmac@pbmac-server $ ./pizza.sh
    * Displaying all pizza names using $@
    Margherita Tomato Panner Gourmet
    
    *Displaying all pizza names using $*
    Margherita,Tomato,Panner,Gourmet

    The exec Command

    The exec command in Linux is used to execute a command from the bash itself. This command does not create a new process, it just replaces the bash with the command to be executed. If the exec command is successful, it does not return to the calling process.

    Syntax:

    exec [-cl] [-a name] [command [arguments]] [redirection ...]
    

    Options:

    • -c: Used to execute the command with empty environment.
    • -a name: Used to pass a name as the zeroth argument of the command.
    • - l: Used to pass dash as the zeroth argument of the command.

    Note: exec command does not create a new process. When we run the exec command from the terminal, the ongoing terminal process is replaced by the command that is provided as the argument for the exec command.

    The exec command can be used in two modes:

    1. Exec with a command as an argument: In the first mode, the exec tries to execute it as a command passing the remaining arguments, if any, to that command and managing the redirections, if any.
    # The given command is executed in a different process...and returns the results, then the original process is back in control
    pbmac@pbmac-server $ exec wc -w < tmp
    9
    pbmac@pbmac-server $
    
    1. Exec without a command: If no command is supplied, the redirections can be used to modify the current shell environment. This is useful as it allows us to change the file descriptors of the shell as per our desire. The process continues even after the exec command unlike the previous case but now the standard input, output, and error are modified according to the redirections.
      pbmac@pbmac-server $ bash
      
      This exec creates a new process that overlays the bash shell started by the previous command
      pbmac@pbmac-server $ exec > tmp
      
      We are now in a different process....and all of this is being directed to the file names tmp by the previous command
      pbmac@pbmac-server $ ls
      pbmac@pbmac-server $ echo "This message will not be displayed"
      pbmac@pbmac-server $ exit
      exit
      
      The exit command exits the other process...and returns to the original context
      We see all of the output that had been sent to the file named tmp
      pbmac@pbmac-server $ cat tmp
      test2.sh
      test.sh
      tmp
      This message will not be displayed

    The source Command

    The source command is a shell built-in command which is used to read and execute the content of a file (generally a set of commands), passed as an argument in the current shell script. After taking the content of the specified files, the command passes it to the TCL interpreter as a text script which then gets executed. If any arguments are supplied, they become the positional parameters when filename is executed. Otherwise, the positional parameters remain unchanged. The entries in $PATH are used to find the directory containing FILENAME, however if the file is not present in $PATH it will search the file in the current directory. The source command has no option and the argument is the file only.

    Syntax:

    source FILENAME [arguments]

    Using the source command is very similar to writing a shell script, but there is no requirement for a shebang line, and the file does NOT have to be set to executable. If there is a file that has the following two lines:

    pbmac@pbmac-server $ cat test-3.txt
    ls
    cal

    Then to source the file:

    pbmac@pbmac-server $ source test-3.txt
    CPP      get-pip.py   Linux           t1       test2
    'CS 11'   Java         myFile.txt.gz   t1.cpp   test2.cpp
    CSP31B   JavaScript   Python          test1    WebStuff
       November 2020      
    Su Mo Tu We Th Fr Sa  
     1  2  3  4  5  6  7  
     8  9 10 11 12 13 14  
    15 16 17 18 19 20 21  
    22 23 24 25 26 27 28  
    29 30  
    

    Adapted from:
    "How to use positional parameters" by Vivek Gite is licensed under CC BY-NC 3.0
    "exec command in Linux with examples" by anindo_7, Geeks for Geeks is licensed under CC BY-SA 4.0
    "source command in Linux with Examples" by Suraj1994, Geeks for Geeks is licensed under CC BY-SA 4.0


    13-C.8: Positional Parameters / exec Command / source Command is shared under a CC BY-SA 4.0 license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?