Shell Variables
- The ideas are somewhat universal, but this is bash.
- The shell maintains a set of variables
- You can see the variables set with printenv
- You can look at the value of a variable with echo $VARNAME
- You can set variables for the current shell
- BOB="hello"
- echo $BOB
- bash
- echo $BOB
- exit
- or for all invocations
- export FRED="hello"
- echo $FRED
- bash
- echo $FRED
- exit
- Environment variables are used all over the place
- To control shells
- $PATH tells the shell where to find executable.
- adding . to $PATH tells it to look in the local directory
- PATH=$PATH:.
- export PATH
- Careful when executing programs in others directories.
- To control programs
- All programs have a third argument to main
- char * envp[]
- This is a null terminated array of strings.
- Write a quick program.
- Shell related tasks
- $? is the exit status of the last program run
- $# the number of arguments to the shell
- $n - the n^th argument to the shell
- A list of standard variables is here
- On startup bash will execute the code in a number of files.
- Normally a system wide file.
- And one or two files in the user's directory.
- Be careful modifying these, you can make it so you can't log in.