Useful variables in Linux, and how they can be utilised/customised. Environment variables are passed to any command/program created by the current shell and are typically denoted in ALL CAPS. Local variables are only available in current shell and are declared in lower case. The echo command can be used to display a variable by prefixing it with $. It may be helpful to delimit variables using {} to make them easier to read. Before modifying an environment variable, it is advisable to back it up to a local variable. Some modifications need to be applied in an initialisation file to become persistent.
echo ${PATH}
#displays the PATH environment variable
path=$PATH
#backs up environment variable PATH to local variable path
?
Can be used to denote exit status of last command – an integer between 0 and 255 with 0 being successful.
#following grep mrnoobot /etc/passwd
echo $?
#displays exit status - eg 0 for success (ie found), 1 for fail -not found, 2 for fail - permission denied and so on.
HISTCONTROL
Variable that contains customisation features for history.
ignoredups
#ignore duplicate commands entered consecutively
ignorespace
#ignore any command preceded by a space
ignoreboth
#combines ignorespace and ignoredups
erasedups
#do not store duplicate commands (only the most recent is retained)
ignorespace:erasedups
#chaining customisation - this provides ignorespace and erasedups
HISTFILE
Provides a path in which the command history is kept – by default contains maximum of 500 entries.
HISTFILESIZE
Determines how many commands to write to ${HISTFILE} when shell is closed. If ${HISTFILESIZE} and ${HISTSIZE} are different, the lower value will be written.
HISTIGNORE
Specifies which commands should be ignored for writing to history.
HISTIGNORE='ls*:cd*:history*:exit'
#ignores the ls, cd and history commands including all trailing syntax, as well as the exit command.
HISTSIZE
Defines how many commands to keep in memory for each shell.
HOME
Contains path to users home directory.
PATH
The PATH environment variable is a list of directories that are searched for commands entered by the user. The PATH variable can be modified; this could be used inappropriately by naming a malicious script after a popular command and including the path in the PATH variable.
PATH=$PATH:/home/mrnoobot
#appends the directory /home/mrnoobot to the PATH variable
It is generally more accepted to move the command into an existing PATH directory. When searching for a command, the system moves through the PATH variable from left to right and uses the first matching command it finds (hence the vulnerability described above). It is therefore important to check to see if a command already exists with that name (using type command) before moving files.
PS1
Prompt variable.
PS1 = 'Noobot> '
#Changes command prompt to Noobot>
Special characters (preceded by \)
w #display full path
W #display relative path
Others are listed under:
man bash
#search PROMPTING
TZ
Timezone variable
export TZ=America/New_York
#set timezone to America New York time (EST). Timezone must be available.