Using Linux Commands 2 Lab5 echo Command Options

Using Linux Commands 2 Lab#5

echo Command • Options -n Do not output the trailing new line. -e Enable interpretation of the following backslash escaped characters in the strings: a alert (bell) b backspace n new line t horizontal tab 2

Examples of quoting special characters ubuntu@ubuntu: ~$ echo –n “operating system” operating system ubuntu@ubuntu: ~$ $ echo –e “operating t system” Operating system $ echo -e "An apple a day keeps away attdoctorn" An apple a day keeps away doctor 3

Sort command • Sort the lines of text files. $ sort file. Name • by default it will sort in normal order(alphabetical 0 -9 A-Z ). • Options • -r Reverse normal order(reverse alphabetical Z-A 9 -0). -n Sort in numeric order(Sorts by the beginning of the number at the beginning of the line. ) -nr Sort in reverse numeric order • Note that this command does not sort the actual file, it just displays the sorted output on your terminal. 4

Sequential commands • To run a sequence of commands type several commands on the same command line and separating them with semicolons(; ) $ mkdir ng ; cd ng ; touch tom 5

date commands Change or set current date and time. Syntax date [date or time string ] Examples • Show current date & time $ date • Set date to 2001 -Mar-15 $date --date=“ 2001 -3 -15” • Set date as well as time $date --date=“ 2001 -3 -15 11: 59 AM” 6

Locating commands • Commands resides in directories. Directory • • Directory Content /bin Contains common Linux user commands such as ls, sort /usr Contains a variety of other user and administrative commands. Contains administrative commands. /sbin The path consists of a list of directories that are checked sequentially for the commands you enter. (When you type a command to run, the system looks for it in the directories specified by PATH in the order specified) To display all command’s paths $ echo $PATH /usr/local/bin: /usr/bin: usr/X 11 R 6/bin: /home/chris/bin • To display the command’s locations $ type command • -a ( if the command reside in several location ) 7

Common shell environment variables • $HOME: your home directory • $PATH: list of directories used to find commands. • $BASH: Contains the full path name of the bash command. • $PS 1: Sets the value of your shell prompt. • $MAIL: This is the location of your mailbox file. • $PWD: This is the directory that is assigned as your current directory. • $OLDPWD: The directory that was the working directory before you changed to the current working directory. • $RANDOM: Accessing this variable causes a random number to be generated. • $UID: The user ID number assigned to your user name. 8

Rerunning commands: command line completion Some of the values you can type partially <Tab>: §Environment variables text begin with $ §User name text begin with ~ §Commands, aliases, or function text begin with regular char. $ echo $PA<tab> $ ls ~/uben<tab> $ ty <tab> 9

Rerunning commands: command line recall • Type a command line shell’s history list history file • To view history list: $ history 8 The last 8 commands, including history 8 • Run command number (!command number) $ !22 If the command # 22 is pwd will be run • Run previous command $ !! 10

Setting your prompt: n Prompt consists of a set of characters that appear each time the shell is ready to accept a command. $ export PS 1=“[t w] $” [20: 26: 32 /var/spool] $ n n n [ precedes a sequence of nonprinting characters. ] Follows a sequence of nonprinting characters. t prints the current time in hours, minutes, and seconds. w display the full path to the current working directory. $ shows the user prompt ($). 11

Adding Alias • makes it possible to launch any command or group of commands by entering a pre-set string of characters. • it allows a user to create simple names or abbreviations (even consisting of just a single character) for commands regardless of how complex the original commands are and then use them in the same way that ordinary commands are used. Use the command unalias to remove alias $ alias p=‘pw d; ls -al’ $ alias rm =‘rm –i’ $ unalias p $ alias del=‘rm –I’ $ alias copy=‘cp’ $ alias rename=‘mv’ $ alias md=‘mkdir’ -i interactive. With this option, rm prompts for confirmation before removing any files. 12

The order in which the shell checks for commands 1 - Aliases 2 - Shell reserved word 3 - Function 4 - Built-in commands 5 - File system commands
- Slides: 13