shell scripting is a simple language that will reduce the repeated work.
To know where bash is located
>where bash
To create a file
>touch name.sh
After creating the file need to change the permission of the file in order to execute the program
To change permission
>chmod +x name.sh
shelling scripting can directly execute with the compilation of code.
To execute the bash program
> ./name.sh
A sample shell scripting program
name.sh
#! /bin/bash
# Hash is used for commenting in shell scripting
echo "Hello World # echo is used to print the text like the print command
Global variable
Commonly used shell scripting predefined system variable list:
- $HOME: This variable represents the current user's home directory.
- $USER or $LOGNAME: These variables typically contain the name of the currently logged-in user.
- $SHELL: It holds the path to the current user's default shell.
- $PWD: This variable stores the present working directory, i.e., the directory from which the script is being run.
- $PATH: It lists directories where the shell looks for executable files. It is a colon-separated list of directories.
- $PS1: This variable defines the primary shell prompt. It's what you see on the command line before typing a command.
- $PS2: The secondary prompt, usually '>', which is displayed when a command is continued on the next line.
- $PS3: The prompt used for the 'select' loop in shell scripts.
- $PS4: The prefix to each line of output when using 'set -x' to enable debugging.
- $IFS: The Internal Field Separator. It's used for word splitting after parameter expansion and is usually set to whitespace characters.
- $RANDOM: Generates a random integer between 0 and 32767.
- $?: Contains the exit status of the last executed command.
- $$: Represents the process ID (PID) of the current shell.
- $0: Refers to the name of the shell or shell script.
- $1, $2, ...: These represent the positional parameters passed to the script or function. $1 is the first argument, $2 is the second argument, and so on.
- $#: Contains the number of positional parameters.
- $@: Represents all the positional parameters as a list.
- $*: Represents all the positional parameters as a single string.
- $!: Contains the PID of the last background command.
- $LINENO: It contains the current line number in the script.
- $COLUMNS and $LINES: These variables store the number of columns and lines in the terminal.
- $UID: Contains the numeric user ID of the current user.
- $EUID: Contains the effective user ID of the current user.
- $GROUPS: Lists the supplementary group IDs of the current user.
- $HOSTNAME: Stores the name of the current host or computer.
Tags
language