Four Bash built-ins


About a week ago i decided to read again the mighty Advanced Bash Scripting Guide. Here follows some notes about four of its (funny) built-ins.


Truncate a file
belch@graal:~$ > file

How does it works?

belch@graal:~$ strace -efile -f bash -c '> file'

.....

open("file", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0666) = 3


Comma separator
belch@graal:~$ t1=0
belch@graal:~$ let t1="t1++, t1+2"
belch@graal:~$ echo $t1
3

Comma separator links together arithmetic expression but only last is returned. On previous example it start by incrementing t1 then sum 2. As you can guess result is 3.


Nop
Believe it or not Bash has a 0x90 builtin. It's the placeholder :

belch@graal:~$ :
belch@graal:~$ echo $?
0


Stacking dirs
belch@graal:~$ cd /home/belch
belch@graal:~$ echo ~+
/home/belch
belch@graal:~$ cd /tmp
belch@graal:~$ cd ~- # ~- get expanded in previous working directory
belch@graal:~$ pwd
/home/belch

0 commenti:

Posta un commento