Some differences between usages of “csh” and “bash” scripting

  1. Useful website links
  2. Shell variables assignment
    bash:

    x=3

     

    csh:

    set x = 3

    Note: spaces are important in bash where as it is not important in csh

  3.  

  4. Environmental variables assignment
    bash:

    export z=5

     

    csh:

    setenv z 5

  5.  

  6. Integer Expression
    bash:

    j=$((i-1))

    Or

    let j=i-1

     

    csh:

    @ j = $i - 1

  7.  

  8. for loop
    bash:

                   for i in 1 2 3
                   do
                   echo Iteration Number $i
                   #let j=i-1
                   j=$((i-1))
                   done
                   

     

    csh:

                  foreach i (1 2 3 )
                    @ j = $i - 1
                  end
                

  9.  

  10. Valid and Invalid Variable Substitution
    ${var} will work in both csh and bash.
    But {$var} work in csh but not in bash.
    See eg:

    csh -c "echo {$HOME}1"

     
    will works in csh but in bash we need the following

    bash -c "echo ${HOME}1"

  11.  

  12. Array: declaration, iterate, first item, count
    bash:

                 snr=(clean1 N1_SNR20 N1_SNR15 N1_SNR10 N1_SNR5 N1_SNR0) # array declaration
                 snr_array_length=${#snr[@]} # get array size
                 snr_first_item=${snr[0]} # get first item, starts at index 0
                 first_item_length=${#snr[0]} # get length of first item
                

     

    csh:

                 set snr = (clean1 N1_SNR20 N1_SNR15 N1_SNR10 N1_SNR5 N1_SNR0) # array declaration
                 set snr_array_length = $#snr # get array size
                 set snr_first_item = $snr[1] # get first item, starts at index 1
                

  13.  

  14. While loop with array
    bash:

                 snr=(clean1 N1_SNR20 N1_SNR15 N1_SNR10 N1_SNR5 N1_SNR0)
                 count=0
                 snr_array_length=${#snr[@]}
                 while [ $count -lt $snr_array_length ]
                 do
                    printf "\n Noise condition: %s\n" ${snr[$count]}
                    let count+=1
                 done
                 

     

    csh:

                 set snr    = (clean1 N1_SNR20 N1_SNR15 N1_SNR10 N1_SNR5 N1_SNR0)
                 set count  = 1
                 while ($count <= $#snr)
                    printf "\n Noise   condition: %s\n" $snr[$count]
                    @ count++
                 end
                 

  15.  

  16. Behaviour of “Exit” command with sourcing
    In bash:
    “exit” command, will exit the shell where the “source” command was executed, as it is that shell which reads each command in turn and executes it. In contrast in a standalone script (if executed, not sourced) the “exit” command will exit only out of the shell/interpreter which was started to execute the commands in the script. Therefore the executed script file will just stop and return to the shell which called it.
    The danger is therefore if a standalone script is sourced instead of being executed. If in that script file there is an “exit” command then the shell which called the script will be exited and not just the script.

    For example, content of ‘sample_bash.sh’ is

                 #!/bin/bash
                 echo "bash-Shell script started"
                 exit
                 echo "bash-Shell script not exited from sample_bash.sh"
                 

    then output of

                 bash -c 'bash sample_bash.sh
                  echo "not exited completely"'
                 

    is

    bash-Shell script started
    not exited completely

     
    But output of

                 bash -c 'source sample_bash.sh
                  echo "not exited completely"'
                 

     

    is

    bash-Shell script started

     

    In csh:
    “exit” command in sourced script, will not behave as expected. It just stop and return to the shell which called it.
     
    For Example, output of

                 csh -c 'source sample_csh.csh
                  echo "not exited completely"'
                 

    is

    C-Shell script started
    not exited completely

  17.  

Leave a comment