Project 56. Cool Shell Tips"How do I override the shell's command-line processing order?" This project gives some miscellaneous tips for the Bash and Tcsh shells. Create Your Own Message of the DayThe message (of the day) you see at the top of every new Terminal windowWelcome to Darwin!is read from the file /etc/motd. It's easy to change this message; simply edit the file. Because of the permissions set on the file, you must edit it as the root user, using the sudo command. Type the following. $ sudo nano /etc/motd Password: ... If you want the message to truly be a Message of the Day, you'll have to change it every day! Format Output with printfThe printf command is much more powerful than echo. It takes a format string and a set of arguments to print within the context of the format string. Let's define a function that takes two arguments and displays them in aligned columns, with the first number in red. The function is written purely to illustrate some tricks we can employ in printf. Type the following. $ function display () { > norm="\033[00m" > red="\033[31m" > indent=${3:-4} > spacer=$((20-$indent)) > printf "%${indent}s%-${spacer}s${red}%7s,${norm}%7s\n" ¬ " " "Values:" $1 $2 > } Learn More
Let's examine the function line by line. First, we define two variables called norm and red. They each hold an ANSI code that changes the color of the text written to Terminal. The sequence \033 is interpreted by printf to be an escape character. We didn't have to define variables; instead, we embedded the sequences in the printf statement. The variable route is neater, especially in real-world scenarios where a function or script contains numerous printf statements. Variable indent is set to the value of the third parameter unless it is undefined, in which case a default value of 4 is assigned. Variable spacer is set to 20-$indent. The sequence $((...)) tells Bash to perform integer arithmetic when evaluating the expression. Finally, we form a printf statement. The first argument is a format string, which, for a default value of 4 for indent, evaluates to printf "%4s%-16s\033[31m%7s,\033[00m%7s\n" "" "Values:" aaa bbb The format string is a sequence of characters that will be displayed onscreen, with special placeholder sequences like %ns and %-ns interspersed. The sequence %ns says to display a string right aligned in an n-character field, and %-ns says to display a string left aligned in an n -character field. After the format string, we must provide a value for each placeholder sequence. In this example, the values are "", "Values:", "aaa", and "bbb". Try the function, and play with some examples yourself to understand what is happening. $ display 3141 2178 6 Values: 3141, 2178 Learn More
Learn More
Prevent Execution of Initialization ScriptsTo launch a login shell from the command line, specify the option --login. $ bash --login To prevent Bash from running /etc/profile and ~/.bash_profile when it is invoked as a login shell, specify the option --noprofile. $ bash --login --noprofile To prevent a non-login shell from running /etc/bashrc and ~/.bashrc, specify option --norc. $ bash --norc Script vs. source vs. .When you execute a script from the command line by typing $ ./name-of-script a new shell instance is launched to execute the script. If you want the script to be executed by the current shellwhen it changes the shell environment, for exampleyou must source it by typing $ source name-of-script or $ . name-of-script Argument RecallRecall the last argument of the last command by typing Escape period (.). Learn More
Auto OptionsIf you always use a particular command with a specific set of its options turned on, an alias can save typing: Just make the generic command name an alias for the option-enabled version. An alias can include its own name in the definition, and Bash understands not to attempt expansion during the definition. $ alias ls='ls -lt' Override Command-Line Processing OrderA very handy trick, should you fall afoul of Bash's command-line processing order, uses the eval command to tell the shell to take a second sweep at processing and expanding a command line. Here's an example in which we are tripped up by the fact that the shell evaluates tilde expansion before variable expansion. $ d="~/Documents" $ ls $d ls: ~/Documents: No such file or directory We solve this problem by telling the shell to evaluate the command line a second time. Now, after the first sweep has expanded $d to ~/Documents, the second sweep will expand tilde to be your home directory. $ eval ls $d AppleWorks User Data Microsoft User Data iChats Downloads Sophie.ichat text TcshTry the following Tcsh built-in command. (There's no space around the dash.) % ls-F If you never want to hear another beep from Tcsh, ever, simply type % set nobeep Learn More
Learn More
|