Bash script

From HPCWIKI
Revision as of 13:16, 28 March 2023 by Admin (talk | contribs)
Jump to navigation Jump to search

A summary for some important Bash scripts

Command Description
set set [options] [arguments][1]

sign (-) is used with the command’s option to enable that option and the plus sign (+) is used with the command’s option to disable that option

-a It defines those variables or functions which are created or modified or exported

Example, These variables v1 and v2 can be accessed after executing the script #!/bin/bash

#Enable -a option to read the values of the variables

set -a

#Initialize three variables

v1=78

v2=50

-b It informs the job termination.
-B To do the task of the brace expansion.
-C It disables the overwriting feature of the existing file

Example, $ cat > testfile.txt

$ set -C

$ cat > testfile.txt. # Could not overwrite testfile.txt

-e It exits for non-zero exit status value.
-f It disables the filename generation task.
-h It saves the location of the command where it has been used.
-m It enables job control.
-n It reads the commands.
-t It exits from the command after executing a single command.
-u It traces the unset variables - error is printed for the uninitialized variable
-v It prints the shell input lines.
-x It displays the commands and their attributes sequentially. It is mainly used to debug the script.
-- variable The string value is divided into three parts based on the space that is printed

#!/bin/bash

#Define a string variable

myvar="Learn bash programming"

#Set the set command without option and with variable

set -- $myvar

#Print the split value

printf "$1\n$2\n$3\n"

Exit Values of Set Command

  1. Zero (0) is returned to complete the task successfully.
  2. One (1) is returned if a failure occurs for any invalid argument.
  3. One (1) is returned if a failure occurs for a missing argument.

Reference