environment variables Flashcards
What command displays the currently set CLI environment variables?
env
What does the term “session” mean?
refers to the state of being logged in to a computer’s command line interface.
When you log in, you start a new session, in which your commands will be recorded and other contextual information will be maintained.
When you close Terminal or type “exit”, your session is closed and that context and data is lost.
How do you set an environment variable on the fly to only affect the current session?
Set the variable on its own line, then use it anywhere:
$ SOMETHING=’some value’
$ echo $SOMETHING
some value
Name some environment files
.bashrc, .bash_profile, .bash_login, .login, .profile
What happens when you open a Terminal or connect to a server?
- You are logged into the command line with a user account. and put in the home dir for that user.
- One or more environment files are executed. These files set environment variables.
E.g. .bashrc, .bash_profile, .bash_login, .login, .profile
what do environment files do?
Store environment variables and allow you to modify or create environment variables.
How do you set an environment variable to temporarily change a variable before it gets used in a command?
Set the variable immediately preceding the command on the same line:
$ SOMETHING='a value' env ... SOMETHING=a value ... NOTE: that var is set ONLY for the executed command
What is the correct syntax to set a variable?
that there should be no spaces between the variable and the equal sign or the equal sign and the value.
E.g.
SOMETHING=’some value’
When setting a value for a variable, are quotations required?
No.
But you must do it when the value has special characters.
what does the export command in an environment files like .bashrc or .bash_profile mean?
It means “make this variable available globally,” thus the variable is available and can be invoked from anywhere in CLI
A variable assignment that starts with export in your .bashrc will be available in the CLI session when you log in.
Note that if an environment variable has been exported once, you don’t need to continually put export before it when you set its value.
Can you use variables as a command?
Yes.
E.g. $ MESSAGE='Hello, world!' $ COMMAND="echo" $ $COMMAND $MESSAGE Hello, world!
When Interpolating Strings (aka displaying variable values in other strings), what must you do to the string that is invoking the variables?
The string must be surrounded in double quotation marks (“), not the single quotes (‘).
E.g.
$ MESSAGE1=”This is message 1.”
$ MESSAGE2=”This is message 2.”
$ MESSAGE=”$MESSAGE1 $MESSAGE2”
What variable determines which directories are searched when a command is entered?
PATH
It tells the CLI session where to find executable files for common commands, such as echo or pwd.
Note: When you type a word into the command line, and it doesn’t start with a /, ~, or a . (because those would indicate a path to an actual file), the command line will search each of the directories listed in the PATH environment variable for that command.
what does bin in /usr/local/bin stand for?
and what is the bin directory?
binary
a standard directory name that contains executable files, or programs.
what command reveals which executable file is executed when a command is run?
which
[which][command]
E.g. which pwd
/bin/pwd
It searches the paths for the named command and displays the first one it finds