Aakash Kushwaha Aakash Kushwaha - 1 year ago 53
Bash Question

using tr command to read permission

Following commands have been executed by me

[root@gblabvl31 Aakash]# cat > file1.txt
-rwx
[root@gblabvl31 Aakash]# cat file1.txt |tr '[-rwx]' '[0421]' > file2.txt
[root@gblabvl31 Aakash]# cat file2.txt
-]]]


I could not understand why the expected output which is

0421


is not achieved

Answer Source

tr is a little special and expects its parameters to already be character classes, thus

$ echo '-rwx' | tr -- '-rwx' '0421'
0421

yields the expected result.