Unix Technical

UNIX / Shell Programming Basic – Beginner Questions Answers

What is $*?

Its mainly used for showing up all params. This show all parameter values passed in shell script

What does $# stand for?

# will return the number of parameters that are passed as the command-line arguments.

What does $? Return?

$? will return exit status of command .0 if command gets successfully executed ,non-zero if command failed.

What are Different types of shells?

sh : the oldest shell 
csh : C shell 
ksh : Korn Shell 
bash : bourne again shell 

How do you read arguments in a shell program – $1, $2?

Shell script accepts parameters in following format…
$1 would be the first command line argument, $2 the second, and so on
$0 is the name of the script or function

If your script has more than 9 params then accept in following way…
${12} : 12th param
${18} : 18th param

What are the different kinds of loops available in shell script?

for, if, while, case

What is the difference between a shell variable that is exported and the one that is not exported?

The Shell variable which is exported would available to all the programs outside the Shell also. And the shell variable which is not exported, would available for that shell or for the shell program only, in which the variable is declared.

Export LANG=C
will make the variable LANG the global variable, put it into the global environment. All other processes can use it. 

will change the value only in the current script. 

If you have a string “one two three”, which shell command would you use to extract the strings?

echo $string | cut -d” ” -f1 
echo $string | cut -d” ” -f2 
echo $string | cut -d” ” -f3 

How will you list only the empty lines in a file (using grep)?

grep “^$” filename.txt

How would you get the character positions 10-20 from a text file?

cat filename.txt | cut -c 10-20


cut -c10-20 <filename.txt>

How would you replace the n character in a file with some xyz?

sed ‘s/n/xyz/g’ filename > new_filename

We can replace n characters by using the following command:
where 1 shows that the search string will start searching patterns from first line of the file.
           ‘.’ for any character.
            g for global replacemet.

What is the difference between a ‘thread’ and a ‘process’?

A process is a collection of virtual memory space, code, data, and system resources. A thread is code that is to be serially executed within a process. A processor executes threads, not processes, so each application has at least one process, and a process always has at least one thread of execution, known as the primary thread. A process can have multiple threads in addition to the primary thread

Thread – is stream of executable code within process. They are light weight process. All thread with in a process  share process instruction,code & data segment,open file descriptor,signal handler,userID and GroupID. Thread has its own set of register including program counter,stack pointer 

What is this line in the shell script do ?#!/bin/ksh?

To invoke the shell indirectly this line is added as the first line in the file.This particular line invokes korn shell

What is use of “cut” command? Give some examples.

Cut – Utility used to cut/Strip out the required data/text from the source.

Cut can be used in three modes,

  Stripping by Character

     cut -c 1-3

  Striping by Byte length

     cut -b -1-72

Stripping by delimiter and fields.

     cut -d “|” -f1


-d “|” -> Delimiter used in input text to separate columns

-f1 -> Field/Column number  

While processing huge input files, Cut’s performance is far better than awk

thanks – shivmohan purohit 

Basic Unix Vi editor command

Receive the latest blog updates

Facebook Twitter Instagram Youtube Linkedin