Very helpful site: http://mywiki.wooledge.org/BashPitfalls
Tag Archives: bash
Splitting Strings in bash
Say you’ve got a pipe ‘|’ separated string; to get each element of it you can use awk. myString=”a|b|c|d” firstElem=$(echo “$myString” | awk ‘{split($0,temp,”|”); print temp[1]}’) firstElem would then contain “a”. Or by using tr: myString=”a|b|c|d” myArray=( $(echo $myString | tr “|” ” “) ) echo ${myArray[0]} # prints a
Remove Windows EOL characters ^M
If you’ve copied/edited a script from a Windows machine you may get a “bad interrupter” error due to Windows EOL characters seen as ^M, sometimes, in vi. To remove them you can use sed like so: $ sed ‘s/^M//g’ Win_File > Unix_File To get ^M you’ll need to enter ‘Ctrl-V Ctrl-M’. You can do the [...]