| Start | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Bash internal variables at a glance
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $ To get the number of command line arguments or positional parameters - type: When run with three arguments the example above will result with the output: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $FUNCNAME To get the name of the current function - type: This instruction will return nothing if you type it outside the function: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $HOME The home directory of the user |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $IFS Contains the Internal Field Separator string that bash uses to split strings when looping etc. The default is the white space characters: The output of the above is: Notes:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $OLDPWD OLDPWD (OLDPrintWorkingDirectory) contains directory before the last |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $OLDPWD OLDPWD (OLDPrintWorkingDirectory) contains directory before the last |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $1 $2 $3 etc... Positional parameters passed to the script from either the command line or a function: The output of the above is: If number of positional argument is greater than nine, curly braces must be used. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $* Will return all of the positional parameters in a single string. testscript.sh: Run the script with several arguments: Output: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $! The Process ID (pid) of the last job run in the background: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $? The exit status of the last executed function or command. Usually 0 will mean OK anything else will indicate a failure: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $$ The Process ID (pid) of the current process: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $HISTSIZE The maximum number of remembered commands: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $BASHPID Process ID (pid) of the current instance of Bash. This is not the same as the |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $BASH_ENV An environment variable pointing to the Bash startup file which is read when a script is invoked. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $BASH_VERSINFO An array containing the full version information split into elements, much more convenient than |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $BASH_VERSION Shows the version of bash that is running, this allows you to decide whether you can use any advanced features: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $EDITOR The default editor that will be involked by any scripts or programs, usually vi or emacs. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $HOSTNAME The hostname assigned to the system during startup. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $HOSTTYPE This variable identifies the hardware, it can be useful in determining which binaries to execute: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $MACHTYPE Similar to |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $OSTYPE Returns information about the type of OS running on the machine, eg. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $PATH The search path for finding binaries for commands. Common examples include When a user or script attempts to run a command, the paths in The directories in So, for example, given the above |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $PPID The Process ID (pid) of the script or shell's parent, meaning the process than invoked the current script or shell. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $SECONDS The number of seconds a script has been running. This can get quite large if shown in the shell: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $SHELLOPTS A readonly list of the options bash is supplied on startup to control its behaviour: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $_ Outputs the last field from the last command executed, useful to get something to pass onwards to another command: test.sh: Output: Note: This is not a foolproof way to get the script path |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $RANDOM Each time this parameter is referenced, a random integer between 0 and 32767 is generated. Assigning a value to this variable seeds the random number generator (source (opens new window)). |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $GROUPS An array containing the numbers of groups the user is in: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $LINENO Outputs the line number in the current script. Mostly useful when debugging scripts. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $SHLVL When the bash command is executed a new shell is opened. The $SHLVL environment variable holds the number of shell levels the current shell is running on top of. In a new terminal window, executing the following command will produce different results based on the Linux distribution in use. Using Fedora 25, the output is "3". This indicates, that when opening a new shell, an initial bash command executes and performs a task. The initial bash command executes a child process (another bash command) which, in turn, executes a final bash command to open the new shell. When the new shell opens, it is running as a child process of 2 other shell processes, hence the output of "3". In the following example (given the user is running Fedora 25), the output of $SHLVL in a new shell will be set to "3". As each bash command is executed, $SHLVL increments by one. One can see that executing the 'bash' command (or executing a bash script) opens a new shell. In comparison, sourcing a script runs the code in the current shell. test1.sh test2.sh run.sh Execute: Output: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $UID A read only variable that stores the users' ID number: |
Read Article
https://develop.horje.com/learn/1434/reference