loader

pwd Command

The “pwd” command is one of the most frequently used commands in the short form of Print Working Directory. Each time users interact with the shell, they work within a directory. So the “pwd” prints the directory that the user is currently working within.

pwd” without any options, displays the full path to the current working directory. It starts with the root “/”.

pwd –P” displays the actual current working directory includes no symbolic links.

pwd –L” displays the symbolic links to the current working directory.

The first line command creates a symbolic link in the example code below. We will cover the symbolic links in the following posts.

				
					sara@main:~$ ln -s ~/Linux ~programs
sara@main:~$ cd ~programs
sara@main:~/~programs$ pwd
/home/sara/~programs
sara@main:~/~programs$ pwd -P
/home/sara/Linux
sara@main:~/~programs$ pwd -L
/home/sara/~programs
sara@main:~/~programs$

				

 Variables in Linux

Every programming language uses variables to simplify programming. Variables keep data within a name, so the programmer can quickly call them by functions in the programming environment. To better understand, think of a variable as a cup and the value as water; we pour water into the cup so we can transport it to another place. If we pour juice instead of water, the cup is still the same, but the value is changed. In Linux systems, administrators either define shell variables that are only available in the current shell or define environment variables that are available in the entire Linux system; in other words, every shell inherits them. Linux uses environment variables to keep some essential general data, such as the current user home directory.
here are some of the most important environment variables:

Environment variable  Value 
USER  The current user’s name 
PATH  Search path for commands 
PWD  The path of the current working directory 
HOME  The current user home directory 
HOSTNAME  The name of the host 
LANG  Current locales setting (default system language) 
EDITOR  Default file editor 
UID  User ID of the current user 
SHELL  The location of the current user’s shell program 

Administrators can see a complete list of environment variables by executing the env or printenv command. If the administrator runs printenv with environment variables as arguments, it will display the environment variable value.

				
					[root@Lpic1-CentOs9 ~]# printenv HOME
/root
[root@Lpic1-CentOs9 ~]# printenv HOME PATH
/root
/root/.local/bin:/root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin

				

How to set variables and display their values in Linux?

Shell variables

Use this format to set a shell variable in bash :
Variable=value
Administrators can use a variable to keep a string using “or append values to the previous values in this format:
Variable=” this is a string value.”
Variable=”${Variable}this is string 2”

echo command 

Echo command prints a line of string that administrators put as an argument. This command is handy in bash scripting. One of its uses is to display variable values, using “$.” As the example code below.

				
					##########setting shell variables#############
[root@Lpic1-CentOs9 ~]# x=3
[root@Lpic1-CentOs9 ~]# echo $x
3
[root@Lpic1-CentOs9 ~]# Y="Linux is Perfect"
[root@Lpic1-CentOs9 ~]# echo $Y
Linux is Perfect
[root@Lpic1-CentOs9 ~]# Y="${Y}XXX${x}"
[root@Lpic1-CentOs9 ~]# echo $Y
Linux is PerfectXXX3

				
Environment variable

To set an environment variable, use export command in this format:

export variable=value

As we mentioned before, administrators can display environment variable values using the printenv command or echo command.

The unset command deletes the environment variable, as shown in the example code below.
unset variable_name

				
					#######################Setting an Environment variable#################
[root@Lpic1-CentOs9 ~]# export WEB_HOME=/tmp/webhome/
[root@Lpic1-CentOs9 ~]# echo $WEB_HOME
/tmp/webhome/
#######################unset an Environment variable#################
[root@Lpic1-CentOs9 ~]# unset WEB_HOME
[root@Lpic1-CentOs9 ~]# echo $WEB_HOME
[root@Lpic1-CentOs9 ~]#
				

A Real-world example

Most of the Third-party vendors produce programs that run out of the /opt directory. So if we take a look at the PATH environment variable, we notice that /opt directory is not on the list. Therefore we have to append /opt to the end of the value. We should use the same format to set values to variables to do this. 


Note that if you forget to put $PATH in the command, the current PATH value will be replaced by the “:/opt.”

				
					********PATH VALUE**********
[root@Lpic1-CentOs9 ~]# echo $PATH
/root/.local/bin:/root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
***********Appending /opt**************
[root@Lpic1-CentOs9 ~]# PATH=$PATH:/opt
[root@Lpic1-CentOs9 ~]# echo $PATH
/root/.local/bin:/root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/opt

				

Leave a Reply

Your email address will not be published. Required fields are marked *