Tagged: bash

Bash command line movement and deletion shortcuts

If you have typed in

Man I was mean, but I'm changing my scene

And then press alt shift b (the shift may not be needed) twice the cursor will go back to the m of my.

(alt shift f moves in the opposite direction, incidentally)

Press alt shift d it will delete the word my. Press it again and it will then delete scene.

Press ctl d and you will delete not the word but a single character under the cursor.

Press ctl k and you will delete everything after the position. ctl u everything before.

Press ctl _ and you will undo your command line edits.

unix bash

Bash: functions and arguments

A function is easily defined in a script as somename() { echo "blar" } which you then call via somename.

You can pass parameters like somename "hi there" and with the definition somename() { echo ${1}!! }.


Bash: case statement

The case statement is simple enough, and notibily works with regular expressions:

  # Uses a regex
  #anything else

esac is case backwards.

unix unix-bash

Using inotifywait with a while loop in bash

Install inotify-tools and then you can wait for a file to change using:

inotifywait -m thefile.txt

The -m flag monitors the file, instead of exiting on the first event. You can use -e to wait for a particular event–see the man page.

But you probably want to do something when that happens. You can pipe the output of the command to a bash while loop:

inotifywait -m thefile.txt | while read file; do echo $file; done

file in this case will have the file name and event name, but you don’t need to use it obviously.

bash bash-while inotifywait unix

Unix shell: For loop

To loop over a set of words, issuing a command for each word do:

    for i in stop start; do service tomcat7 $i; done

This issues the command ‘service tomcat7’ with the paramters ‘stop’ and then ‘start’. You set the word ‘stop’ to the paramater $i, and ‘start’ in the next iteration.

If you’re doing it all on one line you need a semi-colon after the final word, ‘start’ in this instance. And you need ‘do’ before the actual command.

And you need a semi-colon after the command. And finally ‘done’.

This works in both bash and zsh.

unix unix-for-loop unix-zsh unix-bash

Page 1 of 1