Softpanorama
(slightly skeptical) Open Source Software Educational Society

May the source be with you, but remember the KISS principle ;-)

Softpanorama Search

Bash Tips and Tricks

News

Unix Shell Tips and Tricks Recommended Links Advanced filesystem navigation Command completion Annotated List of Bash Enhancements

BASH Debugging

Dotfiles Shell Prompts Functions Shell Aliases Shell Input and Output Redirection Command history reuse IFS
Pipes in Loops Process Substitution in Shell Pushd, popd and dirs Sequences of commands in Unix shell Shell scripts collections Humor Etc

See also Unix Shell Tips and Tricks

The introduction below was adapted from article "Unix Scripting: some Traps, Pitfalls and Recommendations" by Marc Dobson

BASH has some very unintuitive behavior if you source a script and do not provide a path to it: by default, when the command "source filename" does not contain a slash (i.e. does not include a path for the file), BASH searches the $PATH environment variable for the filename, and only if it does not find one there it searches the current directory!!!! Furthermore this happens whether the file has the executable bit set or not . This is counter intuitive to say the least and as an example TCSH does NOT do this search. One can disable this behavior with the BASH "shopt" builtin command:

shopt -u sourcepath

Recommendation 1: when sourcing a script always use a path name for the file or check that the correct script is the first with that name in the path. If the script is in the current directory then use the command "source ./filename".

Recommendation 2: as a precaution you can also put the option to change the default BASH behavior in your ".bash_profile" setup file. Note however that this will not help if you source a file from within an executed script:

shopt -u sourcepath

Recommendation 3: always choose a unique script name.

Unique script names can easily be obtained by prefixing the name with the project name to the script name (e.g. onlinesw_setup for the setup script of the Online Software project). Bad names are for example, setup, configure, etc...

Sourcing versus Executing

The same applies to the sourced file or the standalone script. In the sourced file, an EXIT command, will exit the shell where the SOURCE command was executed, as it is that shell which reads each command in turn and executes it. In contrast in a standalone script (if executed, not sourced) the EXIT command will exit only out of the shell/interpreter which was started to execute the commands in the script. Therefore the executed script file will just stop and return to the shell which called it.

The danger is therefore if a standalone script is sourced instead of being executed. If in that script file there is an EXIT command then the shell which called the script will be exited and not just the script.

As an example take the following two scripts. Script 1 is:

#!/bin/bash

echo "Executing script2"
./script2
if [ $? -eq 0 ]; then
   echo "Executing ls in /tmp/md"
   ls -l /tmp/md
else
   echo "Exiting"
   exit 1
fi

And script 2 is:

#!/bin/bash

echo "In script 2"
if [ -e "/tmp/md" ]; then
   echo "/tmp/md exists"
else
   echo "/tmp/md does not exist"
   exit 1
fi

Both scripts should have the execute bit set. Start a BASH shell by typing bash, and at the next prompt execute script 1. The following output is produced:

If directory /tmp/md exists: If directory /tmp/md does not exist:
Executing script2
In script 2
/tmp/md exists
Executing ls in /tmp/md
total 0
Executing script2
In script 2
/tmp/md does not exist
Exiting

Now change script 1 to source script 2 instead of executing it (source ./script2 instead of ./script2). When the script 1 is executed the following output will be produced:

If directory /tmp/md exists: If directory /tmp/md does not exist:
Executing script2
In script 2
/tmp/md exists
Executing ls in /tmp/md
total 0
Executing script2
In script 2
/tmp/md does not exist

If the directory /tmp/md exists then the output is the same and exactly the same commands were executed. If however the directory /tmp/md does not exist then the script 2 has an EXIT and as it was sourced from script 1, it is actually script 1 which exits without the desired effect, i.e. printing "Exiting". In this case it is not very important but it could have very profound consequences with complex scripts.

The ambiguity in this case is compounded by the difference in coding in the two branches of the IF statement of script 2. For the case when the directory exists the EXIT command is implicit (the script goes to the end and exits normally), whereas for the case when the directory does not exist the EXIT command is explicit (this is the one which causes the exit from script 1).

If the programmer wishes to exit from a sourced script file (as he would with the EXIT command in an executed script), he may do so with:

return [n]
where "[n]" is the return value that can be tested for in the script/shell which sourced the script file (as with the EXIT command). Beware though that the RETURN command is also used to exit a function, therefore make sure that the RETURN command is placed in the appropriate place for the desired effect.

Recommendation 4: If the script is meant to be sourced then do not put a line at the top of the file of the format #!/bin/bash and do not set the execute bit on this file (see the 'Warning' in the previous section if you do not have control over the execute bit).

Recommendation 5: The EXIT command should NEVER be used in a sourced file (sourced script) unless the developer is ABSOLUTELY sure that it does what he intends it to do.

Recommendation 6: some people keep the top line #!/bin/bash to show "humans" which script interpreter the commands in the file are meant for. I would recommend that if a reminder is needed, a naming convention be used such as: sourced file.bash, or sourced file.csh.

Recommendation 7: If the script is meant to be executed, the first line should be of the form #!/bin/bash and the execute bit should be set for this file.

Recommendation 8: The EXIT command can be used in an standalone scripts without any risk with respect to other scripts which might call it.

Recommendation 9: The RETURN command can be used to exit a sourced script file, however be carefull as it is also used to exit functions (either in sourced or executed script files).

The above six recommendations will make it easier for people who do not know the scripts to understand how they should be used, i.e. sourced or executed, without any ambiguity and without having to read and understand the scripts.

Duplicate Functionality

If the same functionality is required (i.e. the same commands) to be sourced in one script and executed in another you can use includes.

Put the commands in a sourced file (as opposed to a standalone script) and source this file from the script which needs to source the commands. Create another file which is this time a standalone script and inside this source the sourced file. Now from the script that needs to execute the commands call the second file that was created.

As an example, find bellow the two files to create. The first file (sourced file) could be:

echo "The commands to run start now"
cd /tmp
ls
cd -
echo "The commands to run finish now"

The second file (the standalone script) could be:

#!/bin/bash

echo "We have been executed"
echo "Running the commands ..."
source ./sourced file
echo "Exiting"

If the commands need to be sourced then call:

source ./sourced file

 and if they need to be executed then use:

./standalone script


Notes:
  • This is a Spartan WHYFF (We Help You For Free) site written by people for whom English is not a native language. Some amount of grammar and spelling errors should be expected.
  • The site contain some broken links as it develops like a living tree... Please try to use Google, Open directory, etc. to find a replacement link (see HOWTO search the WEB for details). We would appreciate if you can mail us a correct link.
Google Search
Open directory

Research Index


Old News ;-)

[Aug 9, 2009] My Favorite bash Tips and Tricks

One last tip I'd like to offer is using loops from the command line. The command line is not the place to write complicated scripts that include multiple loops or branching. For small loops, though, it can be a great time saver. Unfortunately, I don't see many people taking advantage of this. Instead, I frequently see people use the up arrow key to go back in the command history and modify the previous command for each iteration.

If you are not familiar with creating for loops or other types of loops, many good books on shell scripting discuss this topic. A discussion on for loops in general is an article in itself.

You can write loops interactively in two ways. The first way, and the method I prefer, is to separate each line with a semicolon. A simple loop to make a backup copy of all the files in a directory would look like this:

$ for file in * ; do cp $file $file.bak; done

Another way to write loops is to press Enter after each line instead of inserting a semicolon. bash recognizes that you are creating a loop from the use of the for keyword, and it prompts you for the next line with a secondary prompt. It knows you are done when you enter the keyword done, signifying that your loop is complete:

$ for file in *
> do cp $file $file.bak
> done

[Aug 4, 2009] Tech Tip View Config Files Without Comments Linux Journal

I've been using this grep invocation for years to trim comments out of config files. Comments are great but can get in your way if you just want to see the currently running configuration. I've found files hundreds of lines long which had fewer than ten active configuration lines, it's really hard to get an overview of what's going on when you have to wade through hundreds of lines of comments.

$ grep ^[^#] /etc/ntp.conf

The regex ^[^#] matches the first character of any line, as long as that character that is not a #. Because blank lines don't have a first character they're not matched either, resulting in a nice compact output of just the active configuration lines.

The Various bash Prompts by Juliet Kemp

PS4 is the prompt shown when you set the debug mode on a shell script using set -x at the top of the script. This echoes each line of the script to STDOUT before executing it. The default prompt is ++. More usefully, you can set it to display the line number, with:
export PS4='$LINENO+ '

 

It's fairly likely that you already have a personalized setting for PS1, the default bash interaction prompt. But what about the others available: PS2, PS3, and PS4?

PS1 is the default interaction prompt. To set it to give you

username@host:directory$
use
export PS1="u@h w$ "
in your ~/.bash_rc. u is the current username, h the current host, and w the working directory. There's a list of escape codes you can use in the bash man page, or in the Bash Prompt HOWTO.

PS2 is the prompt you get when you extend a command over multiple lines by putting at the end of a line and hitting return. By default it's just >, but you can make this a little more obvious with:

export PS2="more -> "
so it looks like:
juliet@glade:~ $ very-long-command-here 
more -> -with -lots -of -options

PS3 governs the prompt that shows up if you use the select statement in a shell script. The default is #?, so if you do nothing to change that, the select statement will print out the options and then just leave that prompt. Alternatively, use this:

PS3="Choose an option: "
select i in yes maybe no
do 
	# code to handle reply
done
which will output:
1) yes
2) maybe
3) no
Choose an option: 
Far more readable for the user!

Finally, PS4 is the prompt shown when you set the debug mode on a shell script using set -x at the top of the script. This echoes each line of the script to STDOUT before executing it. The default prompt is ++. More usefully, you can set it to display the line number, with:

export PS4='$LINENO+ '

All of these can be made to be permanent changes by setting them in your ~/.bash_profile or ~/.bashrc file. (Note that this probably makes little sense to do for PS3, which is better to set per-script.)

Recovering Deleted Files With lsof By Juliet Kemp

One of the more neat things you can do with the versatile utility lsof is use it to recover a file you've just accidentally deleted.

A file in Linux is a pointer to an inode, which contains the file data (permissions, owner and where its actual content lives on the disk). Deleting the file removes the link, but not the inode itself – if another process has it open, the inode isn't released for writing until that process is done with it.

To try this out, create a test text file, save it and then type less test.txt. Open another terminal window, and type rm testing.txt. If you try ls testing.txt you'll get an error message. But! less still has a reference to the file. So:

> lsof | grep testing.txt
less	4607	juliet  4r  REG 254,4   21  
           8880214 /home/juliet/testing.txt (deleted)
The important columns are the second one, which gives you the PID of the process that has the file open (4607), and the fourth one, which gives you the file descriptor (4). Now, we go look in /proc, where there will still be a reference to the inode, from which you can copy the file back out:
> ls -l /proc/4607/fd/4
lr-x------ 1 juliet juliet 64 Apr  7 03:19 
             /proc/4607/fd/4 -> /home/juliet/testing.txt (deleted)
> cp /proc/4607/fd/4 testing.txt.bk
Note: don't use the -a flag with cp, as this will copy the (broken) symbolic link, rather than the actual file contents.

[Jul 7, 2009] xclip Command-Line Clipboard

xclip (available as a package for Debian and Ubuntu) enables you to interact with the X clipboard directly from the command-line — without having to use the mouse to cut and paste.

This is particularly useful if you're trying to get command-line output over to an e-mail or web page. Instead of scrolling around in the terminal to cut and paste with the mouse, screen by screen, you can use this:

command --arg | xclip
Then go to whichever graphical program you want to paste the input into, and paste with the middle mouse button or the appropriate menu item.

You can also enter the contents of a file straight into xclip:

xclip /path/to/file
and again, can then paste that directly wherever you want it. 

The -o option enables you to operate it the other way around: output the contents of the clipboard straight onto the command line. So, you could, for example, copy a command line from a web page, then use

xclip -o
to output it. To output to a file, use
xclip -o /path/to/file

Use the -selection switch to use the buffer-cut or one of the other selection options, rather than the clipboard default. You can also hook it up to an X display other than the default one (e.g., if you're logged on as a different user on :!) with

xclip -d localhost:1

[Jun 29, 2009] !! provides the ability to rerun long commands which cannot be executed on your current account without prefixing them with sudo.

$ whoami
$ sudo !!

[Mar 14, 2009]  How to Be Faster at the Linux Command Line

hacktux.com

Want to be faster at the Linux command line interface? Since most Linux distributions provide Bash as the default CLI, here are some Bash tricks that will help cut down the amount of typing needed to execute commands. Feel free to comment and share your own speed tricks.

Control-R Through Your History

This is my most used shortcut. Hit Control-R and begin to type a string. You immediately get the last command in your Bash history with that string. Hit Control-R again to cycle further backwards in your history.

For instance, type the following and hit Enter.

grep root /etc/passwd

Then hit Control-R and begin to type 'grep'.

Control-R
(reverse-i-search)`gre': grep root /etc/passwd

When you see the original command listed, hit Enter to execute it. Alternatively, you can also hit the Right-Arrow to edit the command before running it.

Use History Expansion

Bash's command history can be referenced using the exclamation mark. For instance, typing two exclamation marks (!!) will re-execute the last command. The next example executes date twice:

date
!!

If you are interested in more than just the last command executed, type history to see a numbered listing of your Bash's history.

history
  39 grep root /etc/passwd
  40 date
  41 date
  42 history

Since grep root /etc/passwd is command number 39, you can re-execute it like so:

!39

You can also reference Bash's history using a search string. For instance, the following will run the last command that started with 'grep'.

!grep

Note, you can set the number of commands stored in your history by setting HISTSIZE.

export HISTSIZE=1000

You can also wipe your history clear with the -c switch.

history -c

Use History Quick Substitution

Historical commands can be edited and reused with quick substitution. Let's say you grep for 'root' in /etc/passwd:

grep root /etc/passwd

Now, you need to grep for 'root' in /etc/group. Substitute 'passwd' for 'group' in the last command using the caret (^).

^passwd^group

The above command will run:

grep root /etc/group

Comments

Sun, 02/08/2009 - 2:25pm — Anonymous (not verified)

For my backup function, I

For my backup function, I use pass the %F-%R to my date command. This would allow me to make multiple backup copies of a file in one day and have them ordered by date/time.

Keith

Thu, 02/05/2009 - 2:58pm — Anonymous (not verified)

Thankyou for ctrl R I have

Thankyou for ctrl R
I have been using command line for two years and one of
my biggest grips was this issue. I and now flying around the command line
thanks Wed, 02/04/2009 - 5:59pm — Max (not verified)

Nice set of tricks. I knew

Nice set of tricks. I knew most of them already but it refreshed my memory. Thanks.

I find even more handy to have this in ~/.inputrc :
# -------- Bind page up/down wih history search ---------
"\e[5~": history-search-backward
"\e[6~": history-search-forward

I'll take the same example : on the bash prompt, type "gre" and Page up, this will give you "grep root /etc/passwd", the last command that started with "gre". Enter Page up again and it'll show you the previous one. Page down is obvioulsy used to show the next one.

I just noticed that the "set -o vi" trick is messing with this one ^_^ Can't tell you why.

Thu, 02/05/2009 - 5:43am — MaximB (not verified)

Nice stuff... There are some

Nice stuff...

There are some GNU/Linux distributions that already use aliases "built-in" .
like rm which is "rm -i" in rhel5 . So if you want to ignore the alias for known commands like rm for example, just type :

command rm

it will ignore the alias for the command.

[Feb 22, 2009] 10 shortcuts to master bash - Program - Linux - Builder AU By Guest Contributor, TechRepublic | 2007/06/25 18:30:02

If you've ever typed a command at the Linux shell prompt, you've probably already used bash -- after all, it's the default command shell on most modern GNU/Linux distributions.

The bash shell is the primary interface to the Linux operating system -- it accepts, interprets and executes your commands, and provides you with the building blocks for shell scripting and automated task execution.

Bash's unassuming exterior hides some very powerful tools and shortcuts. If you're a heavy user of the command line, these can save you a fair bit of typing. This document outlines 10 of the most useful tools:

  1. Easily recall previous commands

    Bash keeps track of the commands you execute in a history buffer, and allows you to recall previous commands by cycling through them with the Up and Down cursor keys. For even faster recall, "speed search" previously-executed commands by typing the first few letters of the command followed by the key combination Ctrl-R; bash will then scan the command history for matching commands and display them on the console. Type Ctrl-R repeatedly to cycle through the entire list of matching commands.
     

  2. Use command aliases

    If you always run a command with the same set of options, you can have bash create an alias for it. This alias will incorporate the required options, so that you don't need to remember them or manually type them every time. For example, if you always run ls with the -l option to obtain a detailed directory listing, you can use this command:

    bash> alias ls='ls -l' 

    To create an alias that automatically includes the -l option. Once this alias has been created, typing ls at the bash prompt will invoke the alias and produce the ls -l output.

    You can obtain a list of available aliases by invoking alias without any arguments, and you can delete an alias with unalias.
     

  3. Use filename auto-completion

    Bash supports filename auto-completion at the command prompt. To use this feature, type the first few letters of the file name, followed by Tab. bash will scan the current directory, as well as all other directories in the search path, for matches to that name. If a single match is found, bash will automatically complete the filename for you. If multiple matches are found, you will be prompted to choose one.
     

  4. Use key shortcuts to efficiently edit the command line

    Bash supports a number of keyboard shortcuts for command-line navigation and editing. The Ctrl-A key shortcut moves the cursor to the beginning of the command line, while the Ctrl-E shortcut moves the cursor to the end of the command line. The Ctrl-W shortcut deletes the word immediately before the cursor, while the Ctrl-K shortcut deletes everything immediately after the cursor. You can undo a deletion with Ctrl-Y.
     

  5. Get automatic notification of new mail

    You can configure bash to automatically notify you of new mail, by setting the $MAILPATH variable to point to your local mail spool. For example, the command:

    bash> MAILPATH='/var/spool/mail/john'
    bash> export MAILPATH 

    Causes bash to print a notification on john's console every time a new message is appended to John's mail spool.

     

  6. Run tasks in the background

    Bash lets you run one or more tasks in the background, and selectively suspend or resume any of the current tasks (or "jobs"). To run a task in the background, add an ampersand (&) to the end of its command line. Here's an example:

    bash> tail -f /var/log/messages &
    [1] 614

    Each task backgrounded in this manner is assigned a job ID, which is printed to the console. A task can be brought back to the foreground with the command fg jobnumber, where jobnumber is the job ID of the task you wish to bring to the foreground. Here's an example:

    bash> fg 1

    A list of active jobs can be obtained at any time by typing jobs at the bash prompt.
     

  7. Quickly jump to frequently-used directories

    You probably already know that the $PATH variable lists bash's "search path" -- the directories it will search when it can't find the requested file in the current directory. However, bash also supports the $CDPATH variable, which lists the directories the cd command will look in when attempting to change directories. To use this feature, assign a directory list to the $CDPATH variable, as shown in the example below:

    bash> CDPATH='.:~:/usr/local/apache/htdocs:/disk1/backups'
    bash> export CDPATH

    Now, whenever you use the cd command, bash will check all the directories in the $CDPATH list for matches to the directory name.
     

  8. Perform calculations

    Bash can perform simple arithmetic operations at the command prompt. To use this feature, simply type in the arithmetic expression you wish to evaluate at the prompt within double parentheses, as illustrated below. Bash will attempt to perform the calculation and return the answer.

    bash> echo $((16/2))
    8
  9. Customise the shell prompt

    You can customise the bash shell prompt to display -- among other things -- the current username and host name, the current time, the load average and/or the current working directory. To do this, alter the $PS1 variable, as below:

    bash> PS1='\u@\h:\w \@> '
    
    bash> export PS1
    root@medusa:/tmp 03:01 PM>

    This will display the name of the currently logged-in user, the host name, the current working directory and the current time at the shell prompt. You can obtain a list of symbols understood by bash from its manual page.
     

  10. Get context-specific help

    Bash comes with help for all built-in commands. To see a list of all built-in commands, type help. To obtain help on a specific command, type help command, where command is the command you need help on. Here's an example:

    bash> help alias
    ...some help text...

    Obviously, you can obtain detailed help on the bash shell by typing man bash at your command prompt at any time.

How to Be Faster at the Linux Command Line

Want to be faster at the Linux command line interface? Since most Linux distributions provide Bash as the default CLI, here are some Bash tricks that will help cut down the amount of typing needed to execute commands. Feel free to comment and share your own speed tricks.

Control-R Through Your History

This is my most used shortcut. Hit Control-R and begin to type a string. You immediately get the last command in your Bash history with that string. Hit Control-R again to cycle further backwards in your history.

For instance, type the following and hit Enter.

grep root /etc/passwd

Then hit Control-R and begin to type 'grep'.

Control-R
(reverse-i-search)`gre': grep root /etc/passwd

When you see the original command listed, hit Enter to execute it. Alternatively, you can also hit the Right-Arrow to edit the command before running it.

Use History Expansion

Bash's command history can be referenced using the exclamation mark. For instance, typing two exclamation marks (!!) will re-execute the last command. The next example executes date twice:

date
!!

If you are interested in more than just the last command executed, type history to see a numbered listing of your Bash's history.

history
  39 grep root /etc/passwd
  40 date
  41 date
  42 history

Since grep root /etc/passwd is command number 39, you can re-execute it like so:

!39

You can also reference Bash's history using a search string. For instance, the following will run the last command that started with 'grep'.

!grep

Note, you can set the number of commands stored in your history by setting HISTSIZE.

export HISTSIZE=1000

You can also wipe your history clear with the -c switch.

history -c

Use History Quick Substitution

Historical commands can be edited and reused with quick substitution. Let's say you grep for 'root' in /etc/passwd:

grep root /etc/passwd

Now, you need to grep for 'root' in /etc/group. Substitute 'passwd' for 'group' in the last command using the caret (^).

^passwd^group

The above command will run:

grep root /etc/group

Use Vi or Emacs Editing Mode

You can further enhance your abilities to edit previous commands using Vi or Emacs keystrokes. For example, the following sets Vi style command line editing:

set -o vi

After setting Vi mode, try it out by typing a command and hitting Enter.

grep root /etc/passwd

Then, Up-Arrow once to the same command:

Up-Arrow
grep root /etc/passwd

Now, move the cursor to the 'p' in 'passwd' and hit Esc.

grep root /etc/passwd
               ^

Now, use the Vi cw command to change the word 'passwd' to 'group'.

grep root /etc/group

For more Vi mode options, see this list of commands available in Vi mode. Alternatively, If you prefer Emacs, use Bash's Emacs mode:

set -o emacs

Emacs mode provides shortcuts that are available through the Control and Alt key. For example, Control-A takes you to the beginning of the line and Control-E takes you to the end of the line. Here is a list of commands available in Bash's Emacs mode.

Use Aliases and Functions

Bash allows for commands, or sets of commands, to be aliased into a single instruction. Your interactive Bash shell should already load some useful aliases from /etc/profile.d/. For one, you probably have ll aliased to ls -l.

If you want to see all aliases loaded, run the alias Bash builtin.

alias

To create an alias, use the alias command:

alias ll='ls -l'

Here are some other common aliases:

alias ls='ls --color=tty'
alias l.='ls -d .* --color=auto'
alias cp='cp -i'
alias mv='mv -i'

Note that you can also string together commands. The follow will alias gohome as cd , then run ls. Note that running cd without any arguments will change directory to your $HOME directory.

alias gohome='cd; ls'

Better yet, only run ls if the cd is successful:

alias gohome='cd && ls || echo "error($?) with cd to $HOME"'

More complex commands can be written into a Bash function. Functions will allow you to provide input parameters for a block of code. For instance, let's say you want to create a backup function that puts a user inputted file into ~/backups.

backup() {
  file=${1:?"error: I need a file to backup"}
 
  timestamp=$(date '+%m%d%y')
  backupdir=~/backups
 
  [ -d ${backupdir} ] || mkdir -p ${backupdir}
  cp -a ${file} ${backupdir}/$(basename ${file}).${timestamp}
  return $?
}

Like the example above, use functions to automate small, daily tasks. Here is one I use to set my xterm title.

xtitle() {
  unset PROMPT_COMMAND
  echo -ne "\033]0;${@}\007"
}

Of course, you can use functions together with aliases. Here is one I use to set my xterm title to 'MAIL' and then run Mutt.

alias mutt='xtitle "MAIL" && /usr/bin/mutt'

Finally, to ensure that your custom aliases and functions are available each login, add them to your .bashrc.

vim ~/.bashrc

[Apr 2, 2008] 10 shortcuts to master bash - Program - Linux - Builder AU

 2007/06/25 |  Guest Contributor, TechRepublic

1. Easily recall previous commands

Bash keeps track of the commands you execute in a history buffer, and allows you to recall previous commands by cycling through them with the Up and Down cursor keys. For even faster recall, "speed search" previously-executed commands by typing the first few letters of the command followed by the key combination Ctrl-R; bash will then scan the command history for matching commands and display them on the console. Type Ctrl-R repeatedly to cycle through the entire list of matching commands.

... ... ...

5. Get automatic notification of new mail

You can configure bash to automatically notify you of new mail, by setting the $MAILPATH variable to point to your local mail spool. For example, the command:

bash> MAILPATH='/var/spool/mail/john'
bash> export MAILPATH 

Causes bash to print a notification on john's console every time a new message is appended to John's mail spool.

6. Run tasks in the background

Bash lets you run one or more tasks in the background, and selectively suspend or resume any of the current tasks (or "jobs"). To run a task in the background, add an ampersand (&) to the end of its command line. Here's an example:

bash> tail -f /var/log/messages &
[1] 614

Each task backgrounded in this manner is assigned a job ID, which is printed to the console. A task can be brought back to the foreground with the command fg jobnumber, where jobnumber is the job ID of the task you wish to bring to the foreground. Here's an example:

bash> fg 1

A list of active jobs can be obtained at any time by typing jobs at the bash prompt.

7. Quickly jump to frequently-used directories

You probably already know that the $PATH variable lists bash's "search path" -- the directories it will search when it can't find the requested file in the current directory. However, bash also supports the $CDPATH variable, which lists the directories the cd command will look in when attempting to change directories. To use this feature, assign a directory list to the $CDPATH variable, as shown in the example below:

bash> CDPATH='.:~:/usr/local/apache/htdocs:/disk1/backups'
bash> export CDPATH

Now, whenever you use the cd command, bash will check all the directories in the $CDPATH list for matches to the directory name.

8. Perform calculations

Bash can perform simple arithmetic operations at the command prompt. To use this feature, simply type in the arithmetic expression you wish to evaluate at the prompt within double parentheses, as illustrated below. Bash will attempt to perform the calculation and return the answer.

bash> echo $((16/2))
8

... ... ...

10. Get context-specific help

Bash comes with help for all built-in commands. To see a list of all built-in commands, type help. To obtain help on a specific command, type help command, where command is the command you need help on. Here's an example:

bash> help alias
...some help text...

Obviously, you can obtain detailed help on the bash shell by typing man bash at your command prompt at any time.

[Mar 30, 2008] Bash tips and tricks « Richard’s linux, web design and e-learning collection

# Bash tips and tricks for History related preferences
# see http://richbradshaw.wordpress.com/2007/11/25/bash-tips-and-tricks/

# == 1 Lost bash history ==
# the bash history is only saved when you close the terminal, not after each command. fix it..
shopt -s histappend
PROMPT_COMMAND=’history -a’

# == 2. Stupid spelling mistakes ==
# This will make sure that spelling mistakes such as ect instead of etc are ignored.
shopt -s cdspell

# == 3. Duplicate entries in bash history ==
# This will ignore duplicates, as well as ls, bg, fg and exit as well, making for a cleaner bash history.
export HISTIGNORE=”&:ls:[bf]g:exit”

# == 4 Multiple line commands split up in history ==
# this will change multiple line commands into single lines for easy editing.
shopt -s cmdhist

My Favorite bash Tips and Tricks

One thing you can do is redirect your output to a file. Basic output redirection should be nothing new to anyone who has spent a reasonable amount of time using any UNIX or Linux shell, so I won't go into detail regarding the basics of output redirection. To save the useful output from the find command, you can redirect the output to a file:
$ find /  -name foo > output.txt

You still see the error messages on the screen but not the path of the file you're looking for. Instead, that is placed in the file output.txt. When the find command completes, you can cat the file output.txt to get the location(s) of the file(s) you want.

That's an acceptable solution, but there's a better way. Instead of redirecting the standard output to a file, you can redirect the error messages to a file. This can be done by placing a 2 directly in front of the redirection angle bracket. If you are not interested in the error messages, you simply can send them to /dev/null:

This shows you the location of file foo, if it exists, without those pesky permission denied error messages. I almost always invoke the find command in this way.

The number 2 represents the standard error output stream. Standard error is where most commands send their error messages. Normal (non-error) output is sent to standard output, which can be represented by the number 1. Because most redirected output is the standard output, output redirection works only on the standard output stream by default. This makes the following two commands equivalent:

$ find / -name foo > output.txt
$ find / -name foo 1> output.txt

Sometimes you might want to save both the error messages and the standard output to file. This often is done with cron jobs, when you want to save all the output to a log file. This also can be done by directing both output streams to the same file:

$ find / -name foo > output.txt 2> output.txt

This works, but again, there's a better way to do it. You can tie the standard error stream to the standard output stream using an ampersand. Once you do this, the error messages goes to wherever you redirect the standard output:

$ find / -name foo > output.txt 2>&1

 

One caveat about doing this is that the tying operation goes at the end of the command generating the output. This is important if piping the output to another command. This line works as expected:

find -name test.sh 2>&1 | tee /tmp/output2.txt

 

 

but this line doesn't:

find -name test.sh | tee /tmp/output2.txt 2>&1

and neither does this one:

find -name test.sh 2>&1 > /tmp/output.txt

 

I started this discussion on output redirection using the find command as an example, and all the examples used the find command. This discussion isn't limited to the output of find, however. Many other commands can generate enough error messages to obscure the one or two lines of output you need.

Output redirection isn't limited to bash, either. All UNIX/Linux shells support output redirection using the same syntax.

Bash Tip #2 subprocess

Bash bang commands can be used for shortcuts too.

I really only use !$ with the cd command. Here’s some examples, although some not really useful. Just to give you an idea of what it does:

  1. which php (maybe it outputs /usr/local/bin/php)
  2. `!!` /path/to/php_script.php (executes php on the script)

Bash Tips and Tricks 'cd' with style

Something you may have seen before in other systems (the much maligned SCO OSes, for example) is this handy option:

shopt -s cdspell

"This will correct minor spelling errors in a 'cd' command, so that instances of transposed characters, missing characters and extra characters are corrected without the need for retyping."

[Mar 20, 2008] bash Tricks From the Developers of the O'Reilly Network - O'Reilly ONLamp Blog

No more worrying about cases

The best bash tip I can share is very helpful when working on systems that don't allow filenames to differ only in case (like OSX and Windows):

create a file called .inputrc in your home directory and put this line in it:

set completion-ignore-case on

Now bash tab-completion won't worry about case in filenames. Thus 'cd sit[tab]' would complete to 'cd Sites/'

Last argument

You can also use Esc-period and get the last parm of the previous line. You can repeatedly use Esc-period to scroll back through time with them. That turns out to be even better than $! because you can edit it once it shows up on your command line.

should be !$
Instead of $!, use !$, it works much better. :)

$ echo asdf
asdf
$ echo !$
echo asdf
asdf
$ echo $!

$

So $! is an empty variable, while !$ brings back the last argument from the last command.

 

Command substitution
$ for s in `cat server.list`; do ssh $s uptime; done;

Command substution is also done using $(command) notation, which I prefer to the backquotes. It allows commands to be nested (backquotes allow that too, but the inner quotes must be escaped using backslashes, which gets messy.

For example:

$ for s in $(cat server.list); do echo "$s: $(ssh $s uptime)"; done;

or:

# get the uptime for just the first server
$ echo "$(date): $(ssh $(head -1 server.list) uptime)"

 

=====

More key bindings and tricks
Bash will keep a history of the directories you visit, you just have to ask.

You can also always go back to the previous directory you were in by typing cd - without the need to pushd the current directory. Using it more than once cycles between the current and previous directory.

CTRL-A takes you to the beginning of the line and CTRL-E takes you to the end of the line. This is probably basic shell knowledge,

I think it's actually common readline/emacs knowledge, and it works in much more programs than just Bash or a terminal. For instance, you can enable them in Gnome applications by adding the line
gtk-key-theme-name = "Emacs" to the ~/.gtkrc-2.0 file.

Other handy key bindings you can use are:
 

 

There's so much usefull knowledge hidden in Bash that, if you spend any time at the command line, you should really get yourself aquinted with. It saves incredible ammounts of time.

Take for example something I wanted to do yesterday. I wanted to now the number of hits on a certain website. I could have installed a tool to parse the Apache access.log, but this was much easier:

$ cat access.log | cut -d"[" -f2 | cut -d"]" -f1 | cut -d"/" -f2 | uniq -c
28905 Mar
16554 Apr
 

Takes no more than a couple of seconds to write, but saves so much time.

Try reading through the Bash man page. It's huge, but think of all the stuff you'll learn! Or read some online Bash scripting tutorials. Everything from gathering statistics from files to creating thumbnails of images (From the top of my head: for A in *; do convert $A -resize 140x140 th_$A; done) becomes a cinch.

BASH Help - A Bash Tutorial

Flip the Last Two Characters

If you type like me your fingers spit characters out in the wrong order on occasion. ctrl-t swaps the order that the last two character appear in.

Searching Bash History

As you enter commands at the CLI they are saved in a file ~./.bash_history. From the bash prompt you can browse the most recently used commands through the least recently used commands by pressing the up arrow. Pressing the down arrow does the opposite.

If you have entered a command a long time ago and need to execute it again you can search for it. Type the command 'ctrl-r' and enter the text you want to search for.

[Dec 9, 2007] Cool Solutions Bash - Making use of your .bashrc file

Good sample bashrc file


This Is Your Open Enterprise™
Skip to Content United States - EnglishNovell Home LoginDownload
 Products & Solutions
 Services & Support
 Partners & Communities
 Search
           Advanced Search 
SolutionsIdentity, Security and Systems ManagementLinux Operating SystemsWorkgroup CollaborationProducts forIndustriesSmall BusinessProducts A-ZServicesConsultingTrainingSupportCustomer CenterDiscussion ForumsDocumentationKnowledgebasePatches & SecuritySupport by ProductPartnersCertified Partner ProductsFind a PartnerPartner with NovellStrategic PartnersCommunitiesBlogsConnection MagazineCool SolutionsDevelopersNovell Users Intl. (NUI)Partner
> cool solutions home  > cool tools home
Bash - Making use of your .bashrc file
Novell Cool Solutions: Cool ToolRate This Page
Reader Rating    from 4 ratings

Printer Friendlytell a friendDigg This - Slashdot This 


In Brief
A sample .bashrc file.

Vitals
Product Categories: 
Open Enterprise Server
SUSE Linux
SUSE Linux Enterprise Desktop
SUSE Linux Enterprise Server
Functional Categories: 
BASH
Shortcuts
Workgroup
Updated: 23 Oct 2006 
File Size: 6.9KB 
License: GPL 
Download: /coolsolutions/tools/downloads/bashrc.txt 
Publisher: David Crouse 


Disclaimer
Please read the note from our friends in legal before using this file.



Details

I was playing with my .bashrc file again, and was once again impressed by how you can tweak Linux to do what YOU want it to do so easily. I am sure there are tons of other tweaks you can do to your .bashrc file, but I really like some of mine, and thought I would share them. Some of the alias's I created, some I found on the net, and some things in my .bashrc file are just there for fun, like the "# WELCOME SCREEN", although it does serve a purpose for me at the same time, it might not be something everyone would want or need.

For those that don't know what a .bashrc file does: "The ~/.bashrc file determines the behavior of interactive shells." Quoted From: The Advanced Bash Scripting Guide 

Basically , it allows you to create shortcuts (alias's) and interactive programs (functions) that run on the startup of the bash shell or that are used when running an interactive shell. For example, it's much easier to just type: ebrc instead of pico ~/.bashrc (I used the alias ebrc , and it stands for "Edit Bash RC file". I could have also aliased it to just use one letter, making it a VERY fast short cut. The bashrc file allows you to create alias's (shortcuts) to almost anything you want. My list is pretty long, but I'm sure there is someone with a longer list ;)

I have my .bashrc file setup in sections. The following is the breakdown by section of how I keep my list of alias's and functions separated. This is just how I do this, your .bashrc file can be modified to suit YOUR needs, that's the interesting part about the .bashrc file. It's VERY customizable and very easy to change.

Header (So I know when i modified it last and what i was running it on)
Exports (So I can set history size, paths , editors, define colors, etc,)
Sourced Alias's (So I can find those hidden alias's faster)
Workstation Alias's (so i can ssh to local machines quickly)
Remote Server Alias's (so i can ssh to remote servers easily)
Script Alias's (quick links to some of my bashscripts)
Hardware control alias's (so I can control cd/dvd/scanners/audio/etc)
Modified commands (Alias's to normal linux commands with special flags)
Chmod Alias's (makes changing permissions faster)
Alias's for GUI programs (start firefox, etc from command line)
Alias's for xterm and others (open xterm with special settings)
Alias's for Lynx (open lynx with urls - kind of a bash bookmark ;) )
UNused Alias's (Alias's that aren't in use on the system, but that i might use later)
Special functions (more of a function than just an alias..it goes here)
Notes (that should be self explanatory ;) )
Welcome Screen (code to make my bash shell display some stuff as it starts up)

That's how I lay out my .bashrc files. It may not be perfect, but it works well for me. I like making changes in just my .bashrc file and not the global files. I like the .bashrc file because you don't need root permissions to make changes that make your life easier at the bash shell.

The following is my .bashrc file (with some things obviously commented out for security... but most of it should be self explanatory). Anyone with comments/suggestions/ideas feel free to let me know. I'm always looking for new and interesting things to do with the .bashrc file.

Want to know what alias's your bash shell has? Simply type the word alias at the command line. The shell will then print out the list of active alias's to the standard output (normally your screen).

#######################################################
# Dave Crouse's .bashrc file
# www.bashscripts.org
# www.usalug.org
#
# Last Modified 04-08-2006
# Running on OpenSUSE 10
#######################################################


# EXPORTS
#######################################################

PATH=$PATH:/usr/lib/festival/ ;export PATH
export PS1="[\[\033[1;34m\w\[\033[0m]\n[\t \u]$ "
export EDITOR=/usr/bin/pico
export HISTFILESIZE=3000 # the bash history should save 3000 commands
export HISTCONTROL=ignoredups #don't put duplicate lines in the history.
alias hist='history | grep $1' #Requires one input

# Define a few Color's
BLACK='\e[0;30m'
BLUE='\e[0;34m'
GREEN='\e[0;32m'
CYAN='\e[0;36m'
RED='\e[0;31m'
PURPLE='\e[0;35m'
BROWN='\e[0;33m'
LIGHTGRAY='\e[0;37m'
DARKGRAY='\e[1;30m'
LIGHTBLUE='\e[1;34m'
LIGHTGREEN='\e[1;32m'
LIGHTCYAN='\e[1;36m'
LIGHTRED='\e[1;31m'
LIGHTPURPLE='\e[1;35m'
YELLOW='\e[1;33m'
WHITE='\e[1;37m'
NC='\e[0m'              # No Color
# Sample Command using color: echo -e "${CYAN}This is BASH
${RED}${BASH_VERSION%.*}${CYAN} - DISPLAY on ${RED}$DISPLAY${NC}\n"


# SOURCED ALIAS'S AND SCRIPTS
#######################################################

### Begin insertion of bbips alias's ###
source ~/.bbips/commandline/bbipsbashrc
### END bbips alias's ###

# Source global definitions
if [ -f /etc/bashrc ]; then
    . /etc/bashrc
fi

# enable programmable completion features
if [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
fi


# ALIAS'S OF ALL TYPES SHAPES AND FORMS ;)
#######################################################

# Alias's to local workstations
alias tom='ssh 192.168.2.102 -l root'
alias jason='ssh 192.168.2.103 -l root'
alias randy='ssh 192.168.2.104 -l root'
alias bob='ssh 192.168.2.105 -l root'
alias don='ssh 192.168.2.106 -l root'
alias counter='ssh 192.168.2.107 -l root'

# ALIAS TO REMOTE SERVERS
alias ANYNAMEHERE='ssh YOURWEBSITE.com -l USERNAME -p PORTNUMBERHERE'
# My server info removed from above for obvious reasons ;)

# Alias's to TN5250 programs. AS400 access commands.
alias d1='xt5250 env.TERM = IBM-3477-FC env.DEVNAME=D1 192.168.2.5 &'
alias d2='xt5250 env.TERM = IBM-3477-FC env.DEVNAME=D2 192.168.2.5 &'
alias tn5250j='nohup java -jar /home/crouse/tn5250j/lib/tn5250j.jar
2>>error.log &'

# Alias's to some of my BashScripts
alias bics='sh /home/crouse/scripts/bics/bics.sh'
alias backup='sh /home/crouse/scripts/usalugbackup.sh'
alias calc='sh /home/crouse/scripts/bashcalc.sh'
alias makepdf='sh /home/crouse/scripts/makepdf.sh'
alias phonebook='sh /home/crouse/scripts/PHONEBOOK/baps.sh'
alias pb='sh /home/crouse/scripts/PHONEBOOK/baps.sh'
alias ppe='/home/crouse/scripts/passphraseencryption.sh'
alias scripts='cd /home/crouse/scripts'

# Alias's to control hardware
alias cdo='eject /dev/cdrecorder'
alias cdc='eject -t /dev/cdrecorder'
alias dvdo='eject /dev/dvd'
alias dvdc='eject -t /dev/dvd'
alias scan='scanimage -L'
alias playw='for i in *.wav; do play $i; done'
alias playo='for i in *.ogg; do play $i; done'
alias playm='for i in *.mp3; do play $i; done'
alias copydisk='dd if=/dev/dvd of=/dev/cdrecorder' # Copies bit by bit
from dvd to cdrecorder drives.
alias dvdrip='vobcopy -i /dev/dvd/ -o ~/DVDs/ -l'

# Alias's to modified commands
alias ps='ps auxf'
alias home='cd ~'
alias pg='ps aux | grep'  #requires an argument
alias un='tar -zxvf'
alias mountedinfo='df -hT'
alias ping='ping -c 10'
alias openports='netstat -nape --inet'
alias ns='netstat -alnp --protocol=inet | grep -v CLOSE_WAIT | cut
-c-6,21-94 | tail +2'
alias du1='du -h --max-depth=1'
alias da='date "+%Y-%m-%d %A    %T %Z"'
alias ebrc='pico ~/.bashrc'

# Alias to multiple ls commands
alias la='ls -Al'               # show hidden files
alias ls='ls -aF --color=always' # add colors and file type extensions
alias lx='ls -lXB'              # sort by extension
alias lk='ls -lSr'              # sort by size
alias lc='ls -lcr'      # sort by change time
alias lu='ls -lur'      # sort by access time
alias lr='ls -lR'               # recursive ls
alias lt='ls -ltr'              # sort by date
alias lm='ls -al |more'         # pipe through 'more'

# Alias chmod commands
alias mx='chmod a+x'
alias 000='chmod 000'
alias 644='chmod 644'
alias 755='chmod 755'

# Alias Shortcuts to graphical programs.
alias kwrite='kwrite 2>/dev/null &'
alias firefox='firefox 2>/dev/null &'
alias gaim='gaim 2>/dev/null &'
alias kate='kate 2>/dev/null &'
alias suk='kdesu konqueror 2>/dev/null &'

# Alias xterm and aterm
alias term='xterm -bg AntiqueWhite -fg Black &'
alias termb='xterm -bg AntiqueWhite -fg NavyBlue &'
alias termg='xterm -bg AntiqueWhite -fg OliveDrab &'
alias termr='xterm -bg AntiqueWhite -fg DarkRed &'
alias aterm='aterm -ls -fg gray -bg black'
alias xtop='xterm -fn 6x13 -bg LightSlateGray -fg black -e top &'
alias xsu='xterm -fn 7x14 -bg DarkOrange4 -fg white -e su &'

# Alias for lynx web browser
alias bbc='lynx -term=vt100 http://news.bbc.co.uk/text_only.stm'
alias nytimes='lynx -term=vt100 http://nytimes.com'
alias dmregister='lynx -term=vt100 http://desmoinesregister.com'


# SOME OF MY UNUSED ALIAS's
#######################################################

# alias d=`echo "Good Morning Dave. today's date is" | festival --tts;
date +'%A %B %e' | festival --tts`
# alias shrink84='/home/crouse/shrink84/shrink84.sh'
# alias tl='tail -f /var/log/apache/access.log'
# alias te='tail -f /var/log/apache/error.log'


# SPECIAL FUNCTIONS
#######################################################

netinfo ()
{
echo "--------------- Network Information ---------------"
/sbin/ifconfig | awk /'inet addr/ {print $2}'
echo ""
/sbin/ifconfig | awk /'Bcast/ {print $3}'
echo ""
/sbin/ifconfig | awk /'inet addr/ {print $4}'

# /sbin/ifconfig | awk /'HWaddr/ {print $4,$5}'
echo "---------------------------------------------------"
}

spin ()
{
echo -ne "${RED}-"
echo -ne "${WHITE}\b|"
echo -ne "${BLUE}\bx"
sleep .02
echo -ne "${RED}\b+${NC}"
}

scpsend ()
{
scp -P PORTNUMBERHERE "$@"
USERNAME@YOURWEBSITE.com:/var/www/html/pathtodirectoryonremoteserver/;
}


# NOTES
#######################################################

# To temporarily bypass an alias, we preceed the command with a \
# EG:  the ls command is aliased, but to use the normal ls command you would
# type \ls

# mount -o loop /home/crouse/NAMEOFISO.iso /home/crouse/ISOMOUNTDIR/
# umount /home/crouse/NAMEOFISO.iso
# Both commands done as root only.


# WELCOME SCREEN
#######################################################

clear
for i in `seq 1 15` ; do spin; done ;echo -ne "${WHITE} USA Linux Users
Group ${NC}"; for i in `seq 1 15` ; do spin; done ;echo "";
echo -e ${LIGHTBLUE}`cat /etc/SUSE-release` ;
echo -e "Kernel Information: " `uname -smr`;
echo -e ${LIGHTBLUE}`bash --version`;echo ""
echo -ne "Hello $USER today is "; date
echo -e "${WHITE}"; cal ; echo "";
echo -ne "${CYAN}";netinfo;
mountedinfo ; echo ""
echo -ne "${LIGHTBLUE}Uptime for this computer is ";uptime | awk /'up/
{print $3,$4}'
for i in `seq 1 15` ; do spin; done ;echo -ne "${WHITE} http://usalug.org
${NC}"; for i in `seq 1 15` ; do spin; done ;echo "";
echo ""; echo ""The following belong under the "function" section in my .bashrc. Useable as seperate programs, I've integrated them simply as functions for my .bashrc file in order to make them quick to use and easy to modify and find. These are functions that are used to symetrically encrypt and to decrypt files and messages. Some are completely command line, and the last two create gui interfaces to locate the files to encrypt/decrypt. If you create a program out of the functions creating a link via a shortcut/icon on the desktop would create a completely gui based interface to locate and encrypt/decrypt files. Either way, it's an easy way to use gpg.

Requires: zenity, gpg

################### Begin gpg functions ##################
encrypt ()
{
# Use ascii armor
gpg -ac --no-options "$1"
}

bencrypt ()
{
# No ascii armor
# Encrypt binary data. jpegs/gifs/vobs/etc.
gpg -c --no-options "$1"
}

decrypt ()
{
gpg --no-options "$1"
}

pe ()
{
# Passphrase encryption program
# Created by Dave Crouse 01-13-2006
# Reads input from text editor and encrypts to screen.
clear
echo "         Passphrase Encryption Program";
echo "--------------------------------------------------"; echo "";
which $EDITOR &>/dev/null
 if [ $? != "0" ];
     then
     echo "It appears that you do not have a text editor set in your
.bashrc file.";
     echo "What editor would you like to use ? " ;
     read EDITOR ; echo "";
 fi
echo "Enter the name/comment for this message :"
read comment
$EDITOR passphraseencryption
gpg --armor --comment "$comment" --no-options --output
passphraseencryption.gpg --symmetric passphraseencryption
shred -u passphraseencryption ; clear
echo "Outputting passphrase encrypted message"; echo "" ; echo "" ;
cat passphraseencryption.gpg ; echo "" ; echo "" ;
shred -u passphraseencryption.gpg ;
read -p "Hit enter to exit" temp; clear
}

keys ()
{
# Opens up kgpg keymanager
kgpg -k
}

encryptfile ()
{
zenity --title="zcrypt: Select a file to encrypt" --file-selection > zcrypt
encryptthisfile=`cat zcrypt`;rm zcrypt
# Use ascii armor
#  --no-options (for NO gui usage)
gpg -acq --yes ${encryptthisfile}
zenity --info --title "File Encrypted" --text "$encryptthisfile has been
encrypted"
}

decryptfile ()
{
zenity --title="zcrypt: Select a file to decrypt" --file-selection > zcrypt
decryptthisfile=`cat zcrypt`;rm zcrypt
# NOTE: This will OVERWRITE existing files with the same name !!!
gpg --yes -q ${decryptthisfile}
zenity --info --title "File Decrypted" --text "$encryptthisfile has been
decrypted"
}

################### End gpg functions ##################
Novell Cool Solutions (corporate web communities) are produced by WebWise Solutions. www.webwiseone.com

Reader Comments
cool man, really cool. i love such stuffs you know. working in the command line makes you feel like a real linux geek
it's really cool. good job.

AuthorsDocumentationGlossaryKnowledgebaseNovell ConnectionPartner Product GuideSupport ForumsTrainingAppNotes by DateAppNotes by TitleSubmit an AppNoteCool Tools by ProductCool Tools by Tool NameCool Tools by DateCool Tools by CategoryCool Tools by File NameCool Tools by PublisherSubmit a ToolAdvertising in Cool SolutionsTalk to UsSubmit a TipSubmit an AppNoteSubscribeXML/RSS News FeedsFirefox FeedsJavascript News FeedsAccess ManagerAuditBorderManagereDirectoryExteNdIdentity ManagerSecureLoginSentinelZENworksSUSE Linux Enterprise DesktopSUSE Linux Enterprise ServerGroupWiseOpen Enterprise ServerSUSE Linux Enterprise DesktopTeaming + ConferencingIdentity, Security, & Systems ManagementLinux Operating SystemsWorkgroupCool Solutions dot ComCool Solutions HomeResourcesAppNotesCool ToolsGet InvolvedCool Solutions to GoOther CoolsCool BlogsCool Solutions WikiOpen Audio (podcasts)
1.800.529.3400 local numbers
Request Call
Corporate Governance | Legal & Export | Privacy | Subscribe | Feedback | Glossary | RSS | Contact | Printer Friendly
© 2007 Novell, Inc. All Rights Reserved.

xargs,  find and several useful shortcuts

See also Unix Xargs and  Unix Find Command pages.

Re:pushd and popd (and other tricks) (Score:2)
by Ramses0 (63476) on Wednesday March 10, @07:39PM (#8527252) My favorite "Nifty" was when I spent the time to learn about "xargs" (I pronounce it zargs), and brush up on "for" syntax.

    ls | xargs -n 1 echo "ZZZ> "

Basically indents (prefixes) everything with a "ZZZ" string. Not really useful, right? But since it invokes the echo command (or whatever command you specify) $n times (where $n is the number of lines passed to it) this saves me from having to write a lot of crappy little shell scripts sometimes.

A more serious example is:

    find -name \*.jsp | sed 's/^/http:\/\/127.0.0.1/server/g' | xargs -n 1 wget

...will find all your jsp's, map them to your localhost webserver, and invoke a wget (fetch) on them. Viola, precompiled JSP's.

Another:

    for f in `find -name \*.jsp` ; do echo "==> $f" >> out.txt ; grep "TODO" $f >> out.txt ; done

...this searches JSP's for "TODO" lines and appends them all to a file with a header showing what file they came from (yeah, I know grep can do this, but it's an example. What if grep couldn't?) ...and finally...

( echo "These were the command line params"
    echo "---------"
    for f in $@ ; do
          echo "Param: $f"
    done ) | mail -s "List" you@you.com ...the parenthesis let your build up lists of things (like interestingly formatted text) and it gets returned as a chunk, ready to be passed on to some other shell processing function.

Shell scripting has saved me a lot of time in my life, which I am grateful for. :^)

[May 7, 2007] To strip file extensions in bash, like this.rbl --> this


  fname=${file%.rbl}

Last argument reuse

tail -f /tmp/foo
rm !$  # !$ is the last argument to the previous command.

Correction sed style

grep 'wibble' afile | lwss  #typo: meant to type less
!!:s/lw/le #!! is last command string, :s does sed-style  modification. :gs does a global replace

# or for simpler corrections
# n.b. textile screws this up. replace the sup elements with circumflexes.
cat .bash_profilx #typo - meant the x to be an e
<sup>x</sup>e # 'repeat last command, subsituting x for e

touch a{1,2,3,4}b # brace gets expanded to a1b a2b a3b a4b so 4 files get touched

cp file{,.old} # brace gets expanded to file file.old , thus creating a backup.

readline Tips and Tricks

The readline library is used by bash and many other programs to read a line from the terminal, allowing the user to edit the line with standard Emacs editing keys.

Useful Commands and Features

The commands in this section are non-mode specific, unlike the ones listed above.

Aliasing Commands

Once again I like how this topic is covered on freeunix.dyndns.org:8088 in "Customizing your Bash environment" I will quote the section entitled "Aliasses":

Altering the Command Prompt Look and Information

Bash has the ability to change how the command prompt is displayed in information as well as colour. This is done by setting the PS1 variable. There is also a PS2 variable. It controls what is displayed after a second line of prompt is added and is usually by default '> '. The PS1 variable is usually set to show some useful information by the Linux distribution you are running but you may want to earn style points by doing your own modifications.

Here are the backslash-escape special characters that have meaning to bash:

Colours In Bash:

Here is an example borrowed from the Bash-Prompt-HOWTO:

This turns the text blue, displays the time in brackets (very useful for not losing track of time while working), and displays the user name, host, and current directory enclosed in brackets. The "\[\033[0m\]" following the $ returns the colour to the previous foreground colour.

How about command prompt modification thats a bit more "pretty":

This one sets up a prompt like this: [user@host] directory $

Break down:

Each user on a system can have their own customized prompt by setting the PS1 variable in either the .bashrc or .profile files located in their home directories.

Basic and Extended Bash Completion

Basic Bash Completion will work in any bash shell. It allows for completion of:

  1. File Names
  2. Directory Names
  3. Executable Names
  4. User Names (when they are prefixed with a ~)
  5. Host Names (when they are prefixed with a @)
  6. Variable Names (when they are prefixed with a $)

This is done simply by pressing the tab key after enough of the word you are trying to complete has been typed in. If when hitting tab the word is not completed there are probably multiple possibilities for the completion. Press tab again and it will list the possibilities. Sometimes on my machine I have to hit it a third time.

Extended Programmable Bash Completion is a program that you can install to complete much more than the names of the things listed above. With extended bash completion you can, for example, complete the name of a computer you are trying to connect to with ssh or scp. It achieves this by looking through the known_hosts file and using the hosts listed there for the completion. This is greatly customizable and the package and more information can be found here.

Configuration of Programmable Bash Completion is done in /etc/bash_completion. Here is a list of completions that are in my bash_completion file by default.


Links
  1. Bash Prompt HOWTO
  2. Bash Reference Manual
  3. Customizing your Bash environment
  4. Working more productively with bash 2.x
  5. Advancing in the Bash Shell
  6. Bash - Bourne Again SHell
  7. What's GNU: Bash - The GNU Shell
  8. Bash Tips in Gentoo Forums
  9. bash(1) - Linux man page
  10. BASHISH

Learn About Bash Scripting:

  1. Bash by example, Part 1
  2. Bash by example, Part 2
  3. Bash by example, Part 3
  4. Advanced Bash-Scripting Guide
  5. A quick guide to writing scripts using the bash shell

unixtips.org bash tips

bash Nicolas Lidzborski at 19 February, 23:54:09
If you want your xterm or rxvt tille bar to show the username, hostname and current directory and if you uses bash, you can set the PROMPT_COMMAND shell variable. Personally, I use the following command in my /etc/profile:
if [ $TERM = "xterm" ]; then
export PROMPT_COMMAND='echo -ne \
"\033]0;${USER}@${HOSTNAME}: ${PWD}\007"'
fi
The test around the export command is done in order to avoid causing problems in text terms.
 
bash sRp at 19 February, 05:23:23
You can execute bash command a certain number of times by using something similar to the following:
n=0;while test -$n -gt -10; do echo n=$n; n=$[$n+1]; done
That code will print "n=0", "n=1", and so on 10 times.
 
bash sRp at 30 January, 07:18:30
You can use CTRL-_ or CTRL-X, CTRL-U to make undo's at the bash prompt.
 
bash Ian Eure at 29 January, 12:55:02
Bash supports tab-completion. That is, you type the first few characters of a command (or file / directory) and hit tab, and bash automagically completes it for you. For example, if you wanted to run the program WPrefs (Window Maker prefrences util), all you have to do is type WP<tab> and bash will fill in the rest plus a trailing space.
 
bash sRp at 28 January, 01:06:05
Hitting META-P in bash will allow you to search through the bash history.
 
bash sRp at 27 January, 13:24:42
If you find yourself having to cd back and forth between long directory names, bash's pushd is the perfect solution. Start in one of the directories, and the type pushd directory2 to go to the second directory. Now if you type dirs you should see the two directories listed. To switch between these two directories just type pushd +1
 
bash sRp at 27 January, 13:16:39
While using bash, if you have typed a long command, and then realize you don't want to execute it yet, don't delete it. Simply append a # to the beginning of the line, and then hit enter. Bash will not execute the command, but will store it in history so later you can go back, remove the # from the front, and execute it.
 
bash sRp at 27 January, 13:10:05
In the bash shell, CTRL-U will delete everything to the left of the cursor.
 
bash sRp at 27 January, 13:08:22
CTRL-T in bash will transpose two characters; great for typos.
 
bash sRp at 21 January, 05:39:18
Hitting CTRL-W in bash will delete the word just before your cursor. CTRL-Y will yank back in the last deleted word (or words if they were delete consecutively). If you deleted words after you deleted what you wanted to yank back in, and already pressed CTRL-Y, you can use ALT-Y to look through those words.
 
bash Mike Lowrie at 19 January, 07:17:36
Here's another way to change into long directory names in bash. For example, the directory, samba-2.0.0beat2. You can put in cd samb* and it will change to the directory that matches the wildcard.
 
bash Sid Boyce at 19 January, 05:00:05
In the bash shell, you can utilize shortcuts. If your last command started with an l was less xxx, then !l will re-execute it. However, if you had been using lpr and ln as well, and you wanted to run less again, then !le would execute it.
 
bash sRp at 18 January, 21:25:17
In bash, hitting ALT-b will move you back a word, and hitting ALT-f will move you forward a word.
 
bash sRp at 18 January, 21:25:10
Typeing CTRL-l at a bash prompt, will clear the screen, and put the current line at the top of the screen.
 
bash schvin at 17 January, 12:46:44
Turning on the scrolllock in a console will pause or suspend the current command in progress in bash, such as ls, du or mpg123.
bash mulo at 30 September, 21:43:22
To lowercase files in current $PWD #!/bin/sh for x in * do newx=`echo $x | tr "[:upper:]" "[:lower:]";` mv "$x" "$newx" echo "$x --> $newx" done
 
bash Jose at 30 September, 21:43:33
For one fast and effective `clear' use echo e='\ec' It does more that `clear'
 
bash nexz at 30 September, 21:44:50
Finding out all the commands installed on your box? At the prompt, press tab twice and it will ask you if you want to see all the commands. Say y and it will show you all the commands that you installed on your box including shell syntax. Very easy to find out and to familiar yourself with the commands you don't know (btw, this only searches according to path variable set in bash login files). But be careful if you are the root; try --help or man page first before blindly type into it. If all the commands listed are in single column and you can't see the top, edit .bash_profile or .bashrc to include this alias: alias ls="ls -C". Then you should be able to see all. One other alternative might be to increase the buffer for the terminal so that it will hold more characters. Hope this helps!
 
bash Daniel Giribet at 30 September, 21:46:07
Would you like to list only directories (without a long -l listing)?
 
dirs () {
ls -F $1 | grep \/ | sed -e 's/\/$//g'
}


Use 'dirs ' on your bash shell and enjoy!
 
bash sRp at 31 July, 19:23:44
The readline support in the bash shell defaults to emacs editing mode. You can easily switch that to vi mode by issuing the following command: set -o vi.
 
bash Antonio at 8 February, 12:42:20
If you use bash, you can search backwards into its history: hit CTRL-R and start typing what you want to search (it works exactly as in Emacs). If there are lots of similar lines in your history, repetedly typing CTRL-R will browse through them
 
bash irfan ahmed at 23 December, 19:36:34
bash allows you to move between the current directory and the previous directory using the hyphen after the cd command. Say you were in /home/john/pies/american. You give the command cd /home/jack/steak/grilled Now you could back to the ../../american directory using cd -
 
bash hictio at 18 January, 02:30:17
you can clear the screen when you logout, in bash, by adding this to the ~/.bash_logout file:
 
setterm -clear
 
if you don´t have a .bash_logout file, just make one.
 
bash johnnycal at 18 January, 02:31:14
I use cd bla; ls -l bla so much I made a function for it see
 
function see () { cd $1; ls . ; }
 
 
bash Nate Fox at 27 December, 04:32:07
In bash, if you add this:
 
complete -d cd
 
Into your ~/.bash_profile or /etc/profile file, then when you cd, it will only search for directories. So if you have a file called "jiggy" and a directory called "joogy" and those are the only things in the directory, and you type cd and press tab, it will just go into "joogy".
 
bash sRp at 5 September, 17:02:58
Under bash or zsh, if you would like to edit a previous command in a text editor instead of on the command line, use the fc command.
 
bash frodo at 10 April, 04:15:04
Aliasing dir to list just directories can be useful. To do so, do the following:
alias dir='ls -l | grep ^d'
grep in this case searches for a d in the first column of each line.
 
bash HellHound at 14 January, 04:31:20
Another search-in-bash thingy: CTRL+R, this is more "realtime"--when you enter a char/string, it gives you a found match directly.
 
bash Joerg Tretter at
If you want to switch off the "beep" during command line-completion you should add an entry either in your ~/.inputrc or system wide in your /etc/inputrc:

 
for visual signal : set bell-style visible
for absolutely no signal: set bell-style none
 
bash Jason P. Stanford at 20 May, 05:21:59
This is a variation for the "colorful directory listing" hint users, that works "better" under bash. Put the following in $HOME/.bashrc or $HOME/.bash_profile:
function v () { ls -l --color=auto $*; }
function d () { ls --color=auto $*; }
HINT: Think of 'v' as "verbose" and 'd' as "directory". And they're much quicker to type (only a single char), so this should satisfy most unix junkies.

Recommended Links


In case of broken links please try to use Google search. If you find the page please notify us about new location
Google     



Copyright © 1996-2009 by Dr. Nikolai Bezroukov. www.softpanorama.org was created as a service to the UN Sustainable Development Networking Programme (SDNP) in the author free time. Submit comments This document is an industrial compilation designed and created exclusively for educational use and is placed under the copyright of the Open Content License(OPL). Site uses AdSense so you need to be aware of Google privacy policy. Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.

Disclaimer:

Last modified: August 15, 2009