Beginner-Intermediate Unix notes

chmod — Usage : For changing file security.

Background: User permissions are shown in Unix (using ls -l) as a 10 char string



1. The first character signify file type [‘-‘ here signifies file]

2. Next three tells about user’s permission [rwx here means read write and execute]

3. Next three are for permissions of member with same group as user [r– here means users group has read and write but NO execute]

4. Next is for all the other members. [r– signifies only read permissions]

Permissions can be changed in two ways using chmod

1. chmod [g/u/o] [+/- ][r/w/x]

Here g is for “group”, u is for “user”, o is for “other”, + is for adding and – is for removing, r(read), w(write), x(eXecute) permissions.

2. the other mode is more interesting and a bit confusing for beginners. It’s called absolute mode. There are two things to keep in mind here

a. Weight of r/w/x

b. Order of various categories or users

Weight is r –> 4  w–> 2 and x–>1

Order is fixed as u(User)g(Group)o(Other)


chmod 755 <filename> means

“give u(7 = 4 + 2 + 1 => read+write+execute) g(5= 4 + 1 means read+execute)o(same as group) permissions for the file with name <filename>”


Common files that are being used while writing shell scripts

1. /dev/tty –> points to the default terminal via which the shell script is running

2. /dev/null –> used when you don’t care about the output.


Standard I/O , Standard Error and Redirection

Unix takes input from Standard Input , prints back to Standard output and throws error to Standard Error and unless specified otherwise all points to the terminal. With these assumptions it has to be very flexible in changing these three to create real world scripts. This is done via “pipes” and redirections.

“>” operator makes the file following it the new standard output

“<” operator makes the file following it the new standard input.

“&>” for redirecting standard error in csh.

The next feature is pipe, which redirects “flow” from one command to another

The general syntax for using pipe is :

command [Arguments] | nextCommand | nextCommandAgain

so the output from the first command becomes the input to the next command which in turns can generate input to some other command.



Sometimes its desirable that the output from one particular subroutine flows to more than one place, this problem could be handled via Tee

Syntax would look something like

<command1> [Arguments] Tee command2 | command3

so Tee would pass the output of “command1” to both “command2” and “command3”


Export command “To create shell variables “persistent” ”

In real world the files are deep inside some directory structure example “\home\tanmay\dev\quanlib\core\maths\numerics\pdesolver.hs”

Would be cool if I can alias such long path or even better automate the whole changing directory to the level by calling say “goNumerics”

Also some cool applications/ ide uses some global variable to provide including or extra directories. [Erlang uses ERL_LIBS], then it’s probably a good idea to declare them always.

That’s where export command helps.

exporting in your “.profile” or “.bash” or “.bash_rc” file would make sure that whenever you start a new “bash” shell you get all these cool stuff/variable available for you.


Some important command-line characters

1. Background process (‘&’) –> This is one of the most used (by me ) character. Some applications (emacs for example) would always keep itself attached to the terminal in which it is running. Which makes the terminal unusable for other activities unless you exit emacs. Which is annoying if you are in deep directory/file and might want to run some some commands after some edits (compile for example).So solution is to start emacs with an ampersand(‘&’) which would makes it run in background

2. Command Grouping  [()] –> you want the first line of you file to be current timestamp and then following by the output of you shell script one way is to write it like

date >> output.txt

mycommand [args] >> output.txt

other way is to use command grouping as (date; mycommand [args]) >>output.txt

btw ‘>>’ is used for append rather then delete and create the file on the right side.


While automating your build, or may be some other task that you achieve using the shell script, there are two kind of actions that you would probably want to do. (In my opinion these two actions are exhaustive)

1. Run <command1> and then after it runs successfully run <command2> and so on

2. Run <command1> and then run <command2> and so on.

These two effects can be achieved by using conditional executions

&& for 1st case


|| for the second.

<command1> && <command 2>

<command1> || <command 2>





Funtional programming with Scala

My first impression of scala is that it’s too verbose. Thing that could be done in may be 1 line in haskell and 2 lines in erlang takes around 5-10 lines(Might not look like a big difference for 1 line but production codes easily span thousands of line even with the most expressive languages and here the factor is of 5). Anyways, I am still new so might not be my place to judge it.

Here goes implementation of some functions / practice problems in scala.

1. Pascal triangle such that pascal(r,c) such function return value at rth row and cth column

a. Haskel version used ZipWith [Signature ((A->A->A) -> [A] -> [A] -> [A]) which is not in scala so I wrote my own version.

(I don’t like the fact that I was not able to pattern match the arguments to map.. to do –> Find a way to pattern match the tuple)

def zipWith[A] (f: (A,A) => A, l1: List[A], l2: List[A]) : List [A] = {

val zipList =>f(x._1,x._2))


———————– Now goes my implementation of the above function —————————-

def pascal(c: Int, r: Int) : Int  {

def pascalH(pr: List[Int], currentRow :Int): List [Int] =


if (r==currentRow) pr else


val l1  = pr :+ 0

val l2 = List(0) ++ pr

val next_list = zipWith((x:Int,y:Int)=> x+y), l1,l2)



val thePascalRow = pascalH(List(1),0)



—– Note that the helper definition works here because scala (as other functional languages) has lexical closure..

I am still new to scala to could not figure out how to use + instead of that lambda in the zipWith call — if you know please leave a comment

Debugging/Reverse engineering Linux shell script.

(Intended for someone[me] who don’t faint on sight of some assembler code and can look for relevant information from the dump of a binary).

(In draft)


using option vx for debugging shell script might be a good idea when you can not find anything wrong by the visual inspection


say you have a shellscript called DoGreatStuff

called with parameters me you they $4 etc

call it as : set -vf DoGreatStuff me you they $4 etc

Example run for cd for list in my computer is

set -vx ls $1
set -vx ls $1
++ __vte_prompt_command
+++ __vte_osc7
__vte_urlencode “${PWD}”)”
__vte_urlencode “${PWD}”)
__vte_urlencode “${PWD}”
++++ __vte_urlencode /home/tanmay
++++ LANG=C
++++ str=/home/tanmay
++++ ‘[‘ -n /home/tanmay ‘]’
++++ safe=/home/tanmay
++++ printf %s /home/tanmay
++++ str=
++++ ‘[‘ -n ” ‘]’
++++ ‘[‘ -n ” ‘]’
+++ printf ’33]7;file://%s%s\a’ tanmay /home/tanmay
++ printf ’33]0;%s@%s:%s07%s’ tanmay tanmay ‘~’ ”

Ignore other things but you can see that ls is a C program.. (Not a good example here but I hope explains the point here)

Conditional parameters substitution in Unix

As a developer who is coding for future, there are conditions when we encounter uncertainty (Monads !! ) 

Shell scripts provide a way to construct some expressions based on these uncertainties. One way is to use “Dollar-Braces”

syntax for that is ${variable-defaultValue}

so if you start a shell and do 

echo ${myVar-variable not defined}

you would get response as

$> variable not defined

now if you do


echo ${myVar-variable not defined}

you will get


Linux Dollars($)

Linux/Unix shell provides some of very nice functionalities (That should always be kept in mind while developing own software).  While writing shell scripts there are number of inbuilt “special” variables provided for developers. Following are the variables that starts with dollar sign ‘$’

1. $# –> # sign occasionally stands for number and this variable tells number of positional parameters in a shell script eg. myCommand -u -v hello dev

has $# = 4

2. $- –> This one tells you about starting parameters for your shell (bash, korn etc)

if I run it on my bash terminal it gives output of himBH

3. $? –> ? is used to ask questions, here too it’s purpose is to ask: “what happened to last executed command”

0 == success

1 == failure

2 == some diaster

4. $$ –> What is my own PID

5. $! –> bang(!) pattern on shell has a use similar to its use here (for last command) here it stores the answer for “what is the process number of last background command”

6. $0 –> What is my name (shell script’s name) on the terminal it would give you -bash or -korn etc

7. $* –> list of all the shell argument

8 $@ –> Same as $* except when enclosed in double quotes