<<

Line LINUXUSER www.photocase.com

Bash tricks POLISHING A few basic tricks can liven up the command line and add a dash of hour ) in front of the user and computer names, followed by the cur- color to your console. BY HEIKE JURZIK rent , you can assign the variable temporarily by doing this: he shell is the command line in- huhn@huhnix:~> terpreter that interfaces between $ export PS1="[\t]U Tthe user and the system. Among The prompt tells you your user ID, the \u@\h:\> " other things it interprets commands, name of the computer you are working [19:11:06] huhn@huhnix:~> wildcards and variables, links com- on, and the current working directory mands, and passes program output in to (this is the home directory for the cur- The escape sequences used here are \t other tools or to files. Besides the Bourne rent user in our example, denoted by the ( in 24-hour format), \u (username Shell (Sh), Korn Shell (Ksh), C Shell tilde character). If you are working as of the logged in user), \h (hostname up (Csh), and Z Shell (Zsh), the Bourne the system administrator, root, the to the first ) and \w (current working Again Shell, or Bash for short, plays a prompt will look slightly different – on directory). Special escape sequences prominent role on -style systems – systems you can tell you are root give you the ability to color the prompt, it has long established itself as the stan- by the pound sign in the prompt (#): assigning green to a normal user prompt, dard shell on Linux. Working with Bash and red to the administrator’s prompt, is a lot fun if you modify the huhnix:~ # for example. prompt to suit your personal prefer- The Bash manpage has a full list of es- ences, if you are familiar with the many Users can modify the prompt to display cape sequences; and you might also like keyboard shortcuts, and if you add more the date and time, the computer , to check out the Bash Prompt Howto [1]. functionality by defining your own or to use different colors – a few steps aliases and environmental variables. are all it takes to set up your individual Good Memory working environment. The environmen- Bash has a good memory, storing user Attention! tal variable responsible for the appear- input in the (a called Commands are typically entered the ance of the prompt is PS1 (see the “Ev- ~/.bash_history), and letting users recy- shell prompt. A typical prompt looks like erything is Variable” box). If you would cle commands on screen. Pressing the this: like to display the current time (in 24- [Up arrow] key displays the last com-

WWW.LINUX - MAGAZINE.COM ISSUE 70 SEPTEMBER 2006 87 LINUXUSER Command Line

Type the command (or part of shell (and other programs). To add - the command) you are look- satility to this chaos, most distributions ing for at the colon. Bash will do their own thing here, and assign dif- complete commands while ferent priorities to configuration files, or you are typing so that just a include yet more files. few letters might the Using SSH to log in at a virtual con- command you are looking for. sole launches a login shell, is To run a command you have prefaced by a minus sign in the found using this approach, list: just press Enter. Alternatively, keep pressing [Ctrl]+ [R] $ auxwww | bash until you what you are huhnix 3778 0.0 0.3 3056U looking for. Pressing [Esc] lets 1652 tty1 Ss+ May28 0:00U you modify the command be- -bash fore running it. huhnix 4193 0.0 0.3 3068U 1672 pts/1 Ss May28 0:00U Figure 1: Everything is variable – “” outputs a com- Context /bin/bash plete list of Bash environmental variables. The functions we have looked at thus far you The login shell first checks the user’s mand to be typed on your screen. Press- find commands, but taken completely home directory for a ~/.bash_profile. ing the key multiple times takes you out of context. To display more complex If this file is missing, bash looks for farther back in time, and pressing the processes on screen, you can either view ~/.bash_login, and then for /etc/profile [Down arrow] key takes you closer to the history file (~/.bash_history), or run or ~/.profile. the present day. You can modify com- the shell builtin (“Fix Command”). In mands that you recycle in this way, and combination with the -l flag, this tool Interactive Shells run them again by pressing Enter. outputs the last 17 commands by de- In addition to this, there are interactive Of course, Bash’s memory is only as fault. You can add a number to change shells – such as the ones launched good as you tell it to be in the HISTSIZE the default. For example, fc -l -5 outputs from within other shells. These include environmental variable (see the “Every- the last five commands. Xterms or other console programs on thing is Variable” box). You can ask the The script tool provides an alternative graphical user interfaces. Preferences shell to tell you how many slots it has approach to logging a shell session. You for these shells are stored in the private reserved for commands: could script MyLogfile before ~/.bashrc, or in the system global /etc/ launching into an extended shell hack, bash.bashrc file, where you also add $ $HISTSIZE to record any commands you enter and the definitions and environmental 500 the output they create. You can quit the variables discussed in this article. practical logger by pressing [Ctrl]+[D]. Whenever you modify your own When the shell hits this threshold, the ~/.bashrc, you either need to launch a commands at the of the list to Bash Configuration Files new shell, or enter source ~/.bashrc to disappear to way for new ones. Bash comes with a collection of start parse the changes. You can shorten the To avoid wearing out your [Up arrow] files that modify the behavior of the command by entering ~/.bashrc. key, Bash includes a search function. If The alias builtin is an- you press the keyboard shortcut [Ctrl-R] other practical feature that or [Ctrl-S] (see the “Bash Keyboard lets users define commands Shortcuts” box), you can search for- they use regularly, along wards or backwards through the history with their typical of for a command. The prompt changes to options, and assign a reflect this, for example, pressing [Ctrl- shorter and more intuitive R] puts the following prompt on your name to the whole kitten screen: caboodle. Most distri- butions define a few de- (reverse-i-search)`': fault aliases – to find out which aliases your version of Bash knows, type the GLOSSARY alias command at the Builtin: Builtins are not independent - prompt: ecutables, but integrated shell com- mands. Builtins do not have their own $ alias manpage, but are typically described in Figure 2: Environmental variables give users the ability to alias ='ls U the Bash documentation (man bash). modify the behavior of Bash and many other programs. --color=auto'

88 ISSUE 70 SEPTEMBER 2006 WWW.LINUX - MAGAZINE.COM Command Line LINUXUSER

To define an alias for the current shell ple, unalias ll would ditch the alias we You can run the export command to session, just type the alias command, just defined. To make an alias persistent, set a variable. For example, if you want followed by the name for the new short- you need to add the definition to your to run a program in a different language , an equals sign, and the command ~/.bashrc, and parse the file. just this once, temporarily set the LC_ (in quotes), as in: ALL variable, and add the executable Everything is Variable name after a blank. The alias ll='ls -l' Environmental variables let you extend the shell’s feature scope, and modify the LC_ALL=fr_FR firefox The unalias command lets you drop any behavior of certain other programs. The shortcuts you no longer need. For exam- system comes with a set of default vari- command would launch the popular ables, such as the HIST- browser in French for a change. You Table 1: Bash Keyboard Shortcuts SIZE example (in the can also use the unset command to [Ctrl-A] Go back to the start of the input. “Good Memory” section). disable a variable set for the current [Ctrl-E] Jump to the end of the input. You can use the echo com- shell, as in: [Esc-B] Go back one word. mand to output the value [Esc-F] Go forward one word. of the variable, remember- unset LS_COLORS [Ctrl-B] Go back one character. ing to preface the variable [Ctrl-F] Go forward one character. with a dollar sign: To set a variable persistently, add the [Ctrl-K] Delete from the cursor position to the end of export command to ~/.bashrc. Figure 2 the input. echo $HISTSIZE shows a commented Bash setup file with [Ctrl-U] Delete from the cursor position to the start of various examples of useful variables. ■ the line. If you are interested in a [Ctrl-W] Delete the word to the left of the cursor. complete list of all Bash [Ctrl-T] Swap the two characters to the left and below environmental variables, [1] Bash Prompt HOWTO: the cursor. you can type env at the http:// www. tldp. org/ HOWTO/ [Esc-T] Swap the two previous words. command line to output Bash-Prompt-HOWTO/ [Ctrl-L] the content of the terminal window. the list (Figure 1).

advertisement