"[TUTORIAL] Bash Examples"

Published: Thu 14 July 2016

In content.

Bash Examples

This tutorial was written for those who wish to get involved in Linux environment. This tutorial will be updated from time to time.

  • Show where you are Solution

    bash-2.03$ pwd tmp bash-2.03$ export PS1='[\u@\h \w]$ ' [jp@solaris8 /tmp]$

  • Need to find and run a particular command under bash Solution

    $ type which which is hashed (/usr/bin/which) $ type ls ls is aliased to 'ls -F -h' $ type -a ls ls is aliased to 'ls -F -h' ls is /bin/ls $apropos music cms (4) - Creative Music System device driver $ apropos music cms (4) - Creative Music System device driver $ locate apropos /usr/bin/apropos ../../../

  • Produce some output without the default newline that echo provides Solution

    $ printf "%s %s" next prompt next prompt$ or $ echo -n prompt prompt$

  • Saving output from a command

    $ echo fill it up fill it up $ echo fill it up > file.txt $ $ cat file.txt fill it up $

  • Saving out from the ls command Solution

    $ ls /tmp/save.out $ cat /tmp/save.out a.out b.out or $ ls -C /tmp/save.out $ cat /tmp/save.out a.out b.out

  • Redirect output and error messages to different files

$ myprogram 1> messages.out 2> message.err or $ myprogram > messages.out 2> message.err

  • Redirect standard error messages to the same place as standrd output

$ both >& outfile or $ both &> outfile or slightly more verbose $ both > outfile 2>&1

  • append the output rather than clobbering it

$ myprogram >> output.file

  • skip the header of a file

tail +2 lines which prints from line 2 onwards

  • Don't want to save the output into a file;don't want to see it at all

$ find / -name myfile -print 2> /dev/null or $ noisy >/dev/null 2>&1

  • Command chaining

    > $ ls;pwd;bla;goat