What is shell scripting?
What exactly is shell scripting? And what is Bash, Korn, and Expect? I use a few commands in a Linux terminal, is that shell scripting too? Again I am confused what exactly is shell scripting?
5 Answers 5
Your terminal runs a shell , probably bash — korn, csh and others are similar shells with different features and syntax.
While you probably use it mostly to run commands, most shells are an interpreter for command language defined by that shell. Programs in that language is called a shell script. See this howto for an overview of shell scripting in bash.
@sai: You’d do which ksh since «ksh» is the name of the executable. In Bash, I prefer type -a over which because it will show you aliases and functions which may supersede an executable plus each location that an executable exists in directories in your path. Some versions of which don’t give you that much information.
Shell scripting is the process of creating a file containing several shell commands (i.e. ls , cd , grep , etc) than can then be executed.
bash and korn are both shells — they allow you to interface with the computer through a command line, rather than running programs by clicking on icons.
The purpose of shell scripting is to automate repetitive tasks, such as setting up an environment to launch a program, or checking to see if logfiles have changed, or archiving a directory (or set of directories) or any other number of tasks.
«The purpose of shell scripting is to automate repetitive tasks», Ohh you can do so much more then just this. That’s the beauty of the unix design. 128.10.19.20/research/technical_reports/1981/TR%2081-379.pdf
«any other number of tasks» 😉 I guess I could have said «anything you want», since most (all?) shell languages are Turing complete..
Shell scripting lets you automate tasks that you do from the command line.
In addition to being an interaction environment the shell (be it the original Bourne shell ( /bin/sh ) or one of the many alternatives (ksh, csh, bash, zsh, tcsh, . ) with different or extended syntax) provides programming language like features (looping, conditionals, functions, variables. ).
Shell scripting is more or less writing a program in you favorite shell.
The line between using the shell and scripting is fuzzy, but I’d put it near «solving a class of problems by writing some shell code that is smart in some way».
I don’t think the line is fuzzy. Although writing complex command lines is a similar activity, interactively sending commands to the shell is not scripting, while writing them into a file is.
@William: It does get fuzzy since you can use pipes, loops and conditionals on the command line. Whether a condition of being in a file is unconditionally necessary to constitute a «script» is a semantic argument.
A shell allows you create one or more pipelines of processes.
One hallmark of the pipes and filters theme — is combining together the tools — the tools are filters — e.g. ls, egrep, paste, bc, wc. You can create your own filters/tools too — and combine them.
The shell defines the syntax for combining filters using pipes. It also gives you a way to test for condition, via the if keyword — and write loops. You can write functions too.
In short, you can write programs — using tools which are mostly there — and create very powerful, tailor made functionality to suite your needs — very quickly. This is scripting — combine and reuse existing tools as scripts in fruitful ways
For example, consider the pipeline
% seq 100 | paste -s -d ‘*’
1*2*3*4*5*6*7*8*9*10. 98*99*100
The person who wrote the seq filter would not have imagined seq used like above. The seq program, by itself, is simple. This ability to arbitrarily connect seq with paste (or some other filter tool) — makes it an extremely powerful concept.
For example, consider the pipeline
% seq 100 | paste -s -d ‘*’ | bc
93326215443944152681699.
It takes the expression (as above) — and feeds it to bc — yet another filter — bc evaluates the multiplication expression.
In short, a shell script allows you to combine/reuse existing tools — with the flow control mechanisms like if, while. to create very powerful programs in a pretty short time.