LinuxuSer Line: Builtins Sebastian Duda, 123RF

Moving beyond Bash GAMES

even beginners can benefit from a greater understanding of the Bash has a mixture of single-letter options be- ginning with a hyphen (‑) that it inher- shell’s many built-in commands. BY BRUCE BYFIELD ited from earlier incarnations, along with longer options that begin with a double ost GNU/ users know or another shell just like any other com- hyphen (‑ ‑) that were added by the GNU that the command line uses mand. In Gnome, you can add options Project. MBash (Bourne Again Shell) [1], by running Gnome Terminal and creat- However, as a command, bash does but what fewer know is that, by default, ing a custom profile. When the profile is behave in some unusual ways. The first they use the plainest version of Bash. created, select Edit | Profiles | Edit | Title argument after the options can be a file The truth is that Bash is a command & Command. Then check the Run a cus‑ full of commands that Bash runs instead much like any other. It might be the tom command instead of my shell check- of waiting for your input the key- command that runs other commands – a box (Figure 1). In the Custom command board. Even more significantly, as Bash command interpreter – but, just like any field, enter the form of the Bash com- starts, it refers to /etc/ profile, the generic other command, its behavior can be rad- mand you want to run, then, directly Bash profile, and ~/ .profile or ~./ . ically altered by the options you add below it, select what you want to hap- bash_profile, the customized profile in when starting it up or by running some pen when you exit from the command. the home directory of the current ac- of its built-in commands (builtins). Similarly, in KDE, open and count, as well as /etc/ bash.bashrc and Moreover, you can even choose an en- create a new profile by selecting Settings ~/ .bashrc for non-login shells. You can tirely different shell through which to in- | Manage Profile. Highlight the new pro- prevent the reading of profile files by teract with your system. file and click Edit Profile | General to adding the ‑‑noprofile option and of the Admittedly, many of Bash’s options modify the Bash command (Figure 1). If *bashrc files by adding the ‑‑norc option. and builtins [2] are of interest only if you you add the Konsole widget to your Instead, you can force Bash to use a re- are writing scripts. Desktop users are un- panel or folder view, it presents a list of placement for all bashrc files with the likely, for example, to find uses for a profiles from which you can choose to option ‑‑rcfile [file]. login shell or builtins like continue or de‑ suit your needs. Many of Bash’s options are of interest clare. Still, even beginners at the com- mainly to advanced users, although you mand prompt can benefit from learning Options effects might try running ‑‑verbose for a while to more about the options available from The basic structure of the Bash com- see which environment variables are in- the command line. mand is no different from any other voked by each command (Figure 2). Modifying Bash is easy. From any command: bash [options] [arguments]. Many, like those that change the startup computer or , you can Bash And, just like any other command, bash files, modify resources that Bash uses.

88 ISSUE 111 FeBRuaRy 2010 Command Line: Bash Builtins LinuxuSer

Several builtins give you basic information about your system. For instance, the com- mand either tells you whether a command is an or builtin or it gives the path to the executable (Figure 5). You cannot see system commands like apt‑get unless you are logged in as root, but type can satisfy your curiosity about the commands you Figure 1: Whether you run KDE (left) or Gnome (right), you can run a modified version of Bash, or another shell are using and how each is altogether, by making a simple change in your desktop. regarded. For example, cp (copy) is a essential com- However, you might try running the command to move to the subdirectory mand and is placed in /bin, whereas command bash ‑‑debugger ‑O extdebug (Figure 4). Another handy mod is cd‑ sudo, which allows you to perform root to run Bash in debugger mode. Another spell, which tries to correct the misspell- functions from another account, is less moderately advanced option is to start a ings of directory names automatically – essential to your system and is therefore restricted shell with ‑, ‑‑restricted, or ‑O although it usually requires the full path placed in /usr/ bin. Also, you can use restricted_shell. A restricted shell is ex- and fails when you use abbreviations type to check to see whether a command actly what it sounds like: one in which like ./ for the current directory. Similarly, is an alias, although you might prefer to some basic actions are not permitted. dirspell corrects misspellings of directory use the alias command instead. These include changing directories, turn- names in file completion. Another builtin that gives you infor- ing off restrictions with set, and another Another mod for file completion is no‑ mation is jobs, which lists processes and nine or ten actions [3]. caseglob, which ignores the usual dis- whether they are running or stopped – Some system administrators use a re- tinction between upper- and lowercase something that can be hard to tell if you stricted shell to prevent users from mov- letters. Also, checkjobs di- are running ing about in the shell, but this technique rects Bash to display the commands in is a weak security tactic because you do status of running or the background, not need to be much of an expert to start stopped jobs when you especially if a shell that is interactive. Instead, a re- exit Bash. The mailwarn they follow the stricted shell is most valuable when you mod has Bash tell you Figure 3: A restricted shell is just tradition want to test programs that might be when a file containing what it sounds like: a shell in which of not providing buggy or untrusted (Figure 3). By run- mail has been read since some functionality, like changing completion ning such programs in a restricted shell, the last Bash ac- directories, is not allowed. messages. you can hope to minimize any damage cessed it. Entering the that they cause. bare command will give you a list of the Perhaps the most interesting option is Builtin Commands processes owned by the current user ac- ‑O [modification], which affects Bash in Many of Bash’s builtins are for scripting, count and the status of each. If you want a number of simple yet productive ways. which is a topic for another day, but in to end a process because it is misbehav- You can start the same modifications addition to shopt, with its modifications ing, you can enter job ‑l to find its pro- with Bash’s builtin shopt ‑s [option]. of Bash’s behavior, the builtins also in- cess ID, then enter [processID] to Besides the extdebug and restricted_ clude functions that are useful in every- shut it down. shell options already mentioned, some of day desktop computing. Other builtins both give information the most immediately useful modifica- In fact, Bash’s builtins are unavoid- and edit system behavior. For example, tions (mods) affect navigation. For in- able. If you have ever typed to the unadorned command set lists all en- stance, if you run bash ‑O autocd, Bash check which directory you are in or cre- vironmental variables, whereas other interprets the name of a subdirectory ated an alias so that entering ls is equiv- options allow you to turn off or on stan- that is entered as an alias for using the alent to typing ls ‑‑color=auto, then you dard system behavior such as brace ex- have used Bash’s pansion (‑B), which is Bash’s ability to builtins. For ex- replace a variable typed in curly braces ample, you can’t with its value. even navigate at Similarly, ulimit ‑a gives you informa- the prompt with- tion about system resource limitations, Figure 2: If you’ve ever wondered what happens when you run a com‑ out using the cd such as the maximum number of threads mand, run Bash with the verbose option. builtin. allowed or the maximum number of pro-

FeBRuaRy 2010 ISSUE 111 89 Linuxuser Command Line: Bash Builtins

thing: The owner of a file can zsh described as being to Bash what read, write, or execute it, but Bash is to the Windows command line – neither their group nor any- in other words, vastly superior. That is one else except root can do an exaggeration and, in ’s case, is any of these things. If I probably helped by the fact that tcsh is wanted to allow others to the default shell of FreeBSD. But, with- Figure 4: Here, the ‑O option tells Bash to treat a direc‑ read new files, I could enter out a doubt, these shells possess numer- tory name that is entered as the equivalent of changing umask 4477 to change the ous features that Bash either lacks or has to that directory, and it will try to correct directory default. in less sophisticated form. For instance, names that are misspelled. Yet another builtin worth tcsh not only has a scripting syntax simi- knowing about is , lar to that of the programming lan- cesses that a single user can own (Figure which manages the command history guage, but it also has programmable 6). But add a number, and you can reset stored for each user in the .bash-history word completion and spelling correction. the limit. For example, ulimit ‑x 10 sets file in their home directory. The bare Similarly, zsh boasts spell-checking, pro- the number of file locks to 10. You can command lists all the commands entered grammable completion, and multiple re- also choose whether a limit is hard or at the command line, with the oldest direction. soft – that is, whether only the root user listed first. For privacy, you can type his‑ New users might also want to explore can raise it, or anyone can. tory ‑d[position] to remove a specific recently developed shells like [6], Security-minded users might also be command, or history ‑c to clear the entire whose goal is to make working with the interested in the umask utility. Just run- history. command line easier. Fish includes high- ning the umask command without op- lighted syntax and enhanced history and tions or a mode tells you the default per- Alternative Shells tab completion. missions when a new file is created on The more advanced your computing skills, the more benefits you can get from Bash, both in terms of customiza- tion and functionality. The examples given here are no more than an introduc- Figure 5: You can use the type command to tion to some of the diversity you can find Figure 7: Use umask to list or set the default find out whether a command is an alias, when you start moving away from the permissions, either in octal (no option) or built­in, or external command. defaults for Bash. symbolic notation (-S). Once you have thoroughly explored the system. If I run umask on the system Bash, you might want to consider ex- Each of these alternatives takes some I am using currently, I find that the de- ploring other shells. Most major distribu- adjustment, but often commands will be fault is set to 0077 in octal notation, tions package several shells, installing similar enough that you should have whereas if I run umask ‑S, I find that, in their executables into the /bin directory. only minimal trouble adjusting to each symbolic notation, the default permis- To use them, all you need to do is edit shell. Like Bash, with its options and sions are u=rwx,g=, o= (Figure 7). the profiles of the Gnome Terminal or builtins, though, these alternative shells Both of these notations mean the same Konsole and the $BASH environment emphasize one of the most important variable to refer to points about the command line: Just as them. on the desktop, you don’t have to settle Relatively few users for what you’re given at the GNU/​Linux bother with the origi- prompt. If you explore, you will soon nal or discover that usually you can do things with the original C your own way – and the more you learn, shell (csh) or Korn the truer that will be. n shell (ksh), although they still have enough INFO enthusiasts that large [1] Bash: distributions like De- http://www.​­ .​­ org/​­ software/​­ bash/​­ bian include them. [2] Bash builtins: However, most users http://www.​­ faqs.​­ org/​­ docs/​­ bashman/​­ ​ exploring alternative ­bashref_55.html#​­ SEC55​­ shells today are more likely to want the [3] Restricted shell: http://​­www.​­faqs.​ Figure 6: The ulimit ‑a command shows system resource limita‑ added functionality of ­org/docs/​­ bashman/​­ bashref_75.​­ html​­ tions for your computer. The table lists a resource, its measure‑ their successors, such [4] Tcsh: http://www.​­ tcsh.​­ org/​­ Welcome​­ ment, the option to list it separately, and whatever limits are as tcsh [4] or zsh [5]. [5] Zsh: http://www.​­ zsh.​­ org/​­ currently placed on it. Also, you can use the ulimit command to I have heard of [6] Fish: http://fishshell.​­ org/​­ index.​­ ​­ change limits. shells like tcsh and

90 ISSUE 111 February 2010