Variables in Shell Script
Yes, Shell scripts support the use of variables, and we need not define a variable’s type during its declaration. There are two types of variables:
- System Defined variables
- User-Defined Variables.
System-defined variables, also called environment variables, are generally Capitalised. You can view all the current environment variables using the printenv command. User-Defined variables are set by the user, and they exist only during script execution. You can define a variable by simply typing its name and assigning a value with = sign and access a variable by adding a $ before the variable name. Variables are demonstrated in the following example script.
# Accessing an Environment Variable echo $USER # Creating and accessing User defined Variable variable_name="w3wiki" echo $variable_name
How to Create a Shell Script in linux
Shell is an interface of the operating system. It accepts commands from users and interprets them to the operating system. If you want to run a bunch of commands together, you can do so by creating a shell script. Shell scripts are very useful if you need to do a task routinely, like taking a backup. You can list those commands and execute them all with just a single script. Let’s see how you can create a shell script and run it on Linux.