Bash ‘must know’ tips
July 13, 2010, 1:08 am
Filed under: nerd, open source

Usually I read a lot of bash tips, codes, snippets, but some tips are hard to find, let see 2 tips:

Read Line

The ideia is, read each line of a file using bash, some people use terrible ways to do this job.

Bash has a simple way, $(< filename)

echo $(< file.txt)

Apparently the command did’n work, but the fact’s, the command worked, but printed line by line without ‘\n’.

This for show how to work with each line.

for i in $(<file); do echo $i; done

Rename only the extension

This is simple, some tasks need a in file and a out file, the two files can be different, if we need a ‘for’ we need create a good number of files.

Instead of create something like foo.txt and foo.txt.xml, you can create one foo.xml.

Let’s use ${//}  to improve your  work.

$variable =
echo ${variable/foo/bar}

Inside a for, is awesome:

for i in */*.xml; do xml2xsd $i ${i/xml/xsd}; done