Commands and Variables

Executing commands

 

You can execute commands from the command prompt by specifying the name of an executable file, which can either be a binary program or a script. By default the OS comes installed with a number of commands and utilities, which allow you to navigate around the file system, install and delete software, configure and manipulate the system and applications.

 

Running commands is also known as a process. When the user issues a command in the foreground(that is the defayult way that commands are issued) the user first has to wait for the process to complete before he is taken back to the shell prompt, after which the can continue issuing other commands.

 

Note:

A note to bear in mind is that the command prompt is case sensitive, whether that would be for filenames or command scripts. So if you are experiencing difficulties executing your command make sure you double-check your spelling and format of your commands.

 

You can execute a command without arguments or parameters by simply typing in the name of the command that you want to be executed and hit ‘ENTER’. The nature of the output of this method varies from command to command but for the basic commands such as cd  (used to navigate you around directories) it will just move you to the current user’s home directory.

 

Commands with arguments

 

The majority of the commands accept arguments, or some sort of parameter, resulting in altering the commands behavior. For example here we well take a look at the cd command. We can present the following command string to the shell prompt:

 

cd /home/example/public_html

 

What will happen is that the user will be taken to the following path /home/example/public_html

 

After executing this command you will see that your command prompt path will be changed to the new one.

 

Commands with options

 

Apart from using arguments most commands can also accept a list of options (also known as flags, or switches), that change how the command behaves. Since they are regarded as special arguments, flags will directly follow the command in a fashion indicated by indicated by a dash – and then the functionality that you want to give to your command. These functionalities are represented by a single lower/uppercase letter. Furthermore some flags start with two dashes --, followed by a single, multi-character option.

 

Here’s a definitive example on how options work. The ls  command by itself will only provide us with short information about the files in the current directory. If we want a more descriptive output we can use the flat –l (known as ‘long listing’) which in terms provides us with a far more detailed output.

 

If we wan’t to see all of the files in the directory, even the hidden ones (by default files that start with a . are excluded from the ls command’s output and are regarded as hidden files, an example of such file is the .htaccess file) we can use the –a flag. How the command should look:

 

ls –a

 

You can spice it up by combining both of the above flags in the following fashion:

 

ls –la

 

Commands with options and arguments

 

This can be performed with almost any of the commands. Here is a basic example of how we can see all the files (even the hidden . files) located in the home directory with detailed information about them:

 

ls –la /home

 

By this example you can easily judge what the purpose of each part is. ls is the command, -la is the flag and /home is the argument to whom the command is applied.

 

Environment variables

 

If you wish to view the environment variables which are available for the particular terminal session use the env command. It is likely that you will be presented with a lot of information, but try to find the PATH  entry.

 

Example:

PATH=/usr/local/jdk/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/X11R6/bin:/root/bin

 

The PATH specifies to the shell the locations where it can look for executable programs or scripts when a command is called. For example the env command is located in the /usr/bin path and isn’t needed from to specify its path because of this reason, since that path is contained in the PATH environment variable.

 

How to see the value of a variable

 

You can directly view the value of a variable by using the echo command. The syntax should be constructed in the following manner:

 

echo $PATH

 

This will present you with the value of the PATH variable. If you try and access an environment variable that doesn’t exist the shell prompt will simply return an empty string to you.

 

Setting Environment Variables

 

After you have some basic understanding on environment variable, let’s see how you can create new environment variables, and assign values to them. This is done by simply providing the name of the variable that you wish to create, and then use the = symbol, after which you proved the value that you wish to assign to the newly created variable. Here’s an example:

 

VAR=value

 

Make sure that your environment variables don’t start with a digit as this might cause complications.

 

It may come to your attention that all the default system variables are in capital case. This indicated that the variables are used by the Shell and Utilities volume.

 

Note:

If the selected variable already exists, the value that it currently has will be overwritten by the new value that you are assigning, so make sure that you aren’t addressing an already existing environment variable.

 

Now if you wish to alter the content of an already existing variable, you can do that in the following manner.

 

export PATH=$PATH:/ex/scr/bin

 

The specified path /ex/scr/bin will now be added to your environment variable.

 

Regarding the export command – its functionality is to export a given variable so ti will be inherited by a child process. More colloquially said, it allows the use of scripts which reference an exported environment variable from the current session.

 

Note:

The modifications that you perform to the system environment variables is only relative for the current session, once you relog, the changes will be reverted.