Monday, June 21, 2021

Hello world of shell scripts

 

Just started recalling shell scripting again and I thought it would be good the post those somewhere.

So I started with following hello world. Not the simplest script, but it doesnt contain complex stuff. this script contains,

  • How to comment,
  • Output values using echo,
  • Execute some simple commands,
  • Assign output of a command to a variable,
  • print variable values using echo.
Just ran above script to break the inertia and get me started.

lets see some basics about shell scripting.

Most people are confused with the extension of shell scripts at the begining. some scripts have .sh extension some have .csh extension. 

I found below stackoverflow answers are nicely explained about that.


Content from link 1
bash and sh are two different shells. Basically bash is sh, with more features and better syntax. Most commands work the same, but they are different.Bash (bash) is one of many available (yet the most commonly used) Unix shells. Bash stands for "Bourne Again SHell",and is a replacement/improvement of the original Bourne shell (sh).

Content from link 2

.sh is indicative of a script for the Bourne shell (sh) or the Bourne Again shell (bash), which is generally a superset of its predecessor.

.csh is indicative of a script for the C shell (csh), which, being a shell, is largely similar, but significantly different as soon as you start doing anything much more complex than running a series of static commands.

Both shells are generally available on any POSIXy environment, and indeed both are often preinstalled, though bash (and its cousin sh) are a little more ubiquitous in my experience.

I say 'indicative' above, because the idea of a file extension doesn't technically exist and has no semantic or syntactical meaning. A file could be named script.steve or documentation.exe or include.h and still actually be a shell script. For more definitive confirmation, look at the first line of the file. For a script designed to be executed directly, it should start with a shebang line, which starts with an octothorpe (#), a bang (!), and the path to the executable which should run the script. For example:

#!/usr/local/bin/bash

or

#!/usr/bin/python3

 

No comments:

Post a Comment

Optimize you working enviorenment : Single command to create & move to a directory in linux (C Shell, Bash)

Usually move to a directory just after creating is bit of a anxious task specially if the directory name is too long. mkdir long-name-of...