Unix / Linux – Environment ”; Previous Next In this chapter, we will discuss in detail about the Unix environment. An important Unix concept is the environment, which is defined by environment variables. Some are set by the system, others by you, yet others by the shell, or any program that loads another program. A variable is a character string to which we assign a value. The value assigned could be a number, text, filename, device, or any other type of data. For example, first we set a variable TEST and then we access its value using the echo command − $TEST=”Unix Programming” $echo $TEST It produces the following result. Unix Programming Note that the environment variables are set without using the $ sign but while accessing them we use the $ sign as prefix. These variables retain their values until we come out of the shell. When you log in to the system, the shell undergoes a phase called initialization to set up the environment. This is usually a two-step process that involves the shell reading the following files − /etc/profile profile The process is as follows − The shell checks to see whether the file /etc/profile exists. If it exists, the shell reads it. Otherwise, this file is skipped. No error message is displayed. The shell checks to see whether the file .profile exists in your home directory. Your home directory is the directory that you start out in after you log in. If it exists, the shell reads it; otherwise, the shell skips it. No error message is displayed. As soon as both of these files have been read, the shell displays a prompt − $ This is the prompt where you can enter commands in order to have them executed. Note − The shell initialization process detailed here applies to all Bourne type shells, but some additional files are used by bash and ksh. The .profile File The file /etc/profile is maintained by the system administrator of your Unix machine and contains shell initialization information required by all users on a system. The file .profile is under your control. You can add as much shell customization information as you want to this file. The minimum set of information that you need to configure includes − The type of terminal you are using. A list of directories in which to locate the commands. A list of variables affecting the look and feel of your terminal. You can check your .profile available in your home directory. Open it using the vi editor and check all the variables set for your environment. Setting the Terminal Type Usually, the type of terminal you are using is automatically configured by either the login or getty programs. Sometimes, the auto configuration process guesses your terminal incorrectly. If your terminal is set incorrectly, the output of the commands might look strange, or you might not be able to interact with the shell properly. To make sure that this is not the case, most users set their terminal to the lowest common denominator in the following way − $TERM=vt100 $ Setting the PATH When you type any command on the command prompt, the shell has to locate the command before it can be executed. The PATH variable specifies the locations in which the shell should look for commands. Usually the Path variable is set as follows − $PATH=/bin:/usr/bin $ Here, each of the individual entries separated by the colon character (:) are directories. If you request the shell to execute a command and it cannot find it in any of the directories given in the PATH variable, a message similar to the following appears − $hello hello: not found $ There are variables like PS1 and PS2 which are discussed in the next section. PS1 and PS2 Variables The characters that the shell displays as your command prompt are stored in the variable PS1. You can change this variable to be anything you want. As soon as you change it, it”ll be used by the shell from that point on. For example, if you issued the command − $PS1=”=>” => => => Your prompt will become =>. To set the value of PS1 so that it shows the working directory, issue the command − =>PS1=”[u@h w]$” [root@ip-72-167-112-17 /var/www/tutorialspoint/unix]$ [root@ip-72-167-112-17 /var/www/tutorialspoint/unix]$ The result of this command is that the prompt displays the user”s username, the machine”s name (hostname), and the working directory. There are quite a few escape sequences that can be used as value arguments for PS1; try to limit yourself to the most critical so that the prompt does not overwhelm you with information. Sr.No. Escape Sequence & Description 1 t Current time, expressed as HH:MM:SS 2 d Current date, expressed as Weekday Month Date 3 n Newline 4 s Current shell environment 5 W Working directory 6 w Full path of the working directory 7 u Current user’s username 8 h Hostname of the current machine 9 # Command number of the current command. Increases when a new command is entered 10 $ If the effective UID is 0 (that is, if you are logged in as root), end the prompt with the # character; otherwise, use the $ sign You can make the change yourself every time you log in, or you can have the change made automatically in PS1 by adding it to your .profile file. When you issue a command that is incomplete, the shell will display a secondary prompt and wait for you to complete the command and hit Enter again. The default secondary prompt is > (the greater than sign), but can be changed by re-defining the PS2 shell variable − Following is the example which uses the default secondary prompt − $ echo “this is a > test” this is a test $ The example given below re-defines PS2 with a customized prompt − $ PS2=”secondary prompt->” $ echo “this is a secondary prompt->test” this is a test $ Environment Variables Following
Category: unix
Getting Started with Linux ”; Previous Next Let”s start from very begining and the first step of Linux is to boot the system to make it live which allows users to interact with it. So let”s start with System Bootup. System Bootup If you have a computer which has the Linux operating system installed in it, then you simply need to turn on the power of the system to make it live. As soon as you turn on the Linux system (You can say also Linux Machine), it starts booting up and finally it prompts you to log into the system, which is an activity to log into the system and use it for your day-to-day activities. A typical login screen for Ubuntu Linux System looks like as follows: Login Linux When you first connect to a Linux/Unix system, you usually see a Login prompt such as the following − login: To log in Have your userid (user identification) and password ready. Contact your system administrator if you don”t have these yet. Type your userid at the login prompt, then press ENTER. Your userid is case-sensitive, so be sure you type it exactly as your system administrator has instructed. Type your password at the password prompt, then press ENTER. Your password is also case-sensitive. If you provide the correct userid and password, then you will be allowed to enter into the system. Read the information and messages that comes up on the screen, which is as follows. login : amrood amrood”s password: Last login: Sun Jun 14 09:32:32 2009 from 62.61.164.73 $ Once you are successfully logged into the Linux system, you will be provided with a command prompt (sometime called the $ prompt ) where you type all Linux commands. For example, to check calendar, you need to type the cal command as follows − $ cal June 2009 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 $ Following section will discuss few important Linux functions alongwith associated commands. Change Password All Linux systems require passwords to help ensure that your files and data remain your own and that the system itself is secure from hackers and crackers. It is advised to change your password frequently to save it from hackers or any other misuse. Following are the steps to change your password − Step 1 − To start, type passwd command at the command prompt as shown below. Step 2 − Enter your old password, the one you”re currently using. Step 3 − Type in your new password. Always keep your password complex enough so that nobody can guess it. But make sure, you remember it. Step 4 − You must verify the password by typing it again. $ passwd Changing password for amrood (current) Linux password:****** New Linux password:******* Retype new Linux password:******* passwd: all authentication tokens updated successfully $ We have added asterisk (*) here just to show the location where you need to enter the current and new passwords otherwise at your system. It does not show you any character when you type. Most of the Linux variants will show the steps sequence more or less same, but given instructions should be good enough for you to change your password. Listing Directories and Files All the data in Liux is organized into files and all the files are organized into different directories. These directories are organized into a tree-like structure called the filesystem. You can use the Linux ls command to list out all the files or directories available in a directory. Following is the example of using ls command with -l option. $ ls -l total 19621 drwxrwxr-x 2 amrood amrood 4096 Dec 25 09:59 uml -rw-rw-r– 1 amrood amrood 5341 Dec 25 08:38 uml.jpg drwxr-xr-x 2 amrood amrood 4096 Feb 15 2006 univ drwxr-xr-x 2 root root 4096 Dec 9 2007 urlspedia -rw-r–r– 1 root root 276480 Dec 9 2007 urlspedia.tar drwxr-xr-x 8 root root 4096 Nov 25 2007 usr -rwxr-xr-x 1 root root 3192 Nov 25 2007 webthumb.php -rw-rw-r– 1 amrood amrood 20480 Nov 25 2007 webthumb.tar -rw-rw-r– 1 amrood amrood 5654 Aug 9 2007 yourfile.mid -rw-rw-r– 1 amrood amrood 166255 Aug 9 2007 yourfile.swf $ Here entries starting with d….. represent directories. For example, uml, univ and urlspedia are directories and rest of the entries are files. Changing Directories While working with different files and directories you will need to go in different directories. You can go inside a directory using the cd command as follows: $ cd uml $ Above command will take you inside uml directory, where you can list available directories and files. Once you are done with your work in a directory, you can go back to parent directory using cd .. as follows: $ cd .. $ Linux uses a single dot . to represent current directory and double dots .. to represent a parent directory. Who Are You? While you”re logged into the system, you might be willing to know : Who am I? or you would like to know whose login you are using at present? The easiest way to find out “who you are” is to enter the whoami command as follows − $ whoami amrood $ Try it on your system. This command lists the account name associated with the current login. You can try who am i command as well to get information about yourself. $ who am i root pts/2 2024-04-24 19:22 (49.205.240.120) $ Who is Logged in? Sometime you might be interested to know who is logged in to the computer at the same time. There are three commands available to get you this information, based on how much you wish to know about the other users: users, who, and w. $ users amrood bablu qadir $ who amrood pts1 Oct 8 14:10 (limbo) bablu pts2