System Variable in ShellScript

Created and maintained by Linux bash shell itself. This type of variable (with the exception of auto_resume and histchars) is defined in CAPITAL LETTERS. You can configure aspects of the shell by modifying system variables such as PS1, PATH, LANG, HISTSIZE, and DISPLAY, etc.

There are several commands available that allow you to list and set environment variables in Linux:

  • env - The command allows you to run another program in a custom environment without modifying the current one. When used without an argument it will print a list of the current environment variables.

  • printenv - The command prints all or the specified environment variables.

  • set - The command sets or unsets shell variables. When used without an argument it will print a list of all variables including environment and shell variables, and shell functions.

  • unset - The command deletes shell and environment variables.

  • export - The command sets environment variables

    .

Below are some of the most common environment variables:

  • USER - The current logged in user.
  • HOME - The home directory of the current user.
  • EDITOR - The default file editor to be used. This is the editor that will be used when you type edit in your terminal.
  • SHELL - The path of the current user's shell, such as bash or zsh.
  • LOGNAME - The name of the current user.
  • PATH - A list of directories to be searched when executing commands. When you run a command the system will search those directories in this order and use the first found executable.
  • LANG - The current locales settings.
  • TERM - The current terminal emulation.
#!/bin/bash
# System define variables.
# echo ${SHELL}
echo ${HOME} # will show the home directory of current user
echo ${OSTYPE} # type type of operating system.
echo $PATH # A list of directories to be searched when executing commands.
echo ${$} # process id
echo ${PPID} # parent process id

echo $PWD # present working directory
echo $HOSTNAME # hostname of machine.
echo $UID # user id
# UID="5000"
echo $UID
sleep 5
echo ${SECONDS}

now let's run the above shell script and see the output.

┌──(gaurav㉿learning-ocean)-[~/shellscript-youtube]
└─$ ./variable.sh
Saurav
my name is Saurav and my age is 20
i am programming
/home/kali
linux-gnu
/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/home/kali/.dotnet/tools
87788
68426
/home/kali/shellscript-youtube
learning-ocean
1000
1000
5

Demo Video

Click Here for Demo Video