Bash shell scripting basic question regarding the syntax and basename

24,280

Solution 1

$0 is just an internal bash variable. From man bash:

   0      Expands  to  the  name  of  the shell or shell
          script.  This is set at shell  initialization.
          If bash is invoked with a file of commands, $0
          is set to the name of that file.  If  bash  is
          started  with the -c option, then $0 is set to
          the first argument after the string to be exe‐
          cuted,  if  one  is present.  Otherwise, it is
          set to the file name used to invoke  bash,  as
          given by argument zero.

So, $0 is the full name of your script, for example /home/user/scripts/foobar.sh. Since you often don't want the full path but just the name of the script itself, you use basenameto remove the path:

#!/usr/bin/env bash

echo "\$0 is $0"
echo "basename is $(basename $0)"

$ /home/terdon/scripts/foobar.sh 
$0 is /home/terdon/scripts/foobar.sh
basename is foobar.sh

The ; is only really needed in bash if you write multiple statements on the same line. It is not needed anywhere in your example:

#!/usr/bin/env bash

## Multiple statements on the same line, separate with ';'
for i in a b c; do echo $i; done

## The same thing on  many lines => no need for ';'
for i in a b c
do 
  echo $i
done

Solution 2

What does $0 mean?

From man bash, section PARAMETERS, subsection Special Parameters:

   0      Expands to the name of the shell or shell script.  This  is  set
          at shell initialization.  If bash is invoked with a file of com‐
          mands, $0 is set to the name of that file.  If bash  is  started
          with  the  -c option, then $0 is set to the first argument after
          the string to be executed, if one is present.  Otherwise, it  is
          set  to  the file name used to invoke bash, as given by argument
          zero.

It says 0 there, but $0 is meant because $ identifies a parameter.

You can find such information in man pages easily by using the search key /. Just type /\$0 followed by return. The $ has to be quoted as \$ in this case because the $ has a special meaning when searching, and the backslash is needed to "escape" this special meaning.

In what cases ";" is used at the end of statement in a script?

Usually only when you want to put at least two statements in a single line, for example this would be a case where you can use ;:

if [ $i = $myname ]; then

There's no case in your example script where the ; is required, you can remove it from the first line. Details can again be found in man bash:

Lists
   A  list  is a sequence of one or more pipelines separated by one of the
   operators ;, &, &&, or ||, and optionally terminated by one of ;, &, or
   <newline>.

   [...]

   A sequence of one or more newlines may appear in a list  instead  of  a
   semicolon to delimit commands.

   If  a  command  is terminated by the control operator &, the shell exe‐
   cutes the command in the background in a subshell.  The shell does  not
   wait  for  the command to finish, and the return status is 0.  Commands
   separated by a ; are executed sequentially; the shell  waits  for  each
   command  to terminate in turn.  The return status is the exit status of
   the last command executed.
Share:
24,280

Related videos on Youtube

Gokul
Author by

Gokul

Updated on September 18, 2022

Comments

  • Gokul
    Gokul over 1 year

    Consider the script below:

    myname=`basename $0`;
    for i in `ls -A`
    do
     if [ $i = $myname ]
     then
      echo "Sorry i won't rename myself"
     else
      newname=`echo $i |tr a-z A-Z`
      mv $i $newname
     fi
    done
    

    1) I am aware that basename $0 signifies my script name here. But how? the syntax explanation please. What does $0 mean?

    2) In what cases ";" is used at the end of statement in a script? For example, the first line of script ends with ; , whereas the 8th line doesn't. Also, i found that adding/removing semi-colons at the end of some lines (for example: 1st/6th/8th lines) doesn't really have any significance and script runs fine with or without it.