What is Shell?
# Online Bash Shell. # Code, Compile, Run and Debug Bash script online. # Write your code in this editor and press 'Run' button to execute it. This download is the full version of the text editor for Windows and includes all features. Click here for Mac and Linux version Download UltraEdit v27.10 (released 2020-09-23).
Shell is a UNIX term for an interface between a user and an operating system service. Shell provides users with an interface and accepts human-readable commands into the system and executes those commands which can run automatically and give the program's output in a shell script.
An Operating is made of many components, but its two prime components are -
- Kernel
- Shell
A Kernel is at the nucleus of a computer. It makes the communication between the hardware and software possible. While the Kernel is the innermost part of an operating system, a shell is the outermost one.
A shell in a Linux operating system takes input from you in the form of commands, processes it, and then gives an output. It is the interface through which a user works on the programs, commands, and scripts. A shell is accessed by a terminal which runs it.
When you run the terminal, the Shell issues a command prompt (usually $), where you can type your input, which is then executed when you hit the Enter key. The output or the result is thereafter displayed on the terminal.
The Shell wraps around the delicate interior of an Operating system protecting it from accidental damage. Hence the name Shell.
When we start to learn the command-line interface, we generally learn and explore it interactively. That is, we enter one command at a time so that we can see the results of each command.
- Can split the editing windows. Compares 2 text files. $49.99-UltraEdit: HTML,PHP CSS C SAS code PL/SQL UNIX Shell Scripts Visual Basic: Windows,Linux, Mac OS: Integrated SSH, FTP, and Telnet. Multi-caret editing. Support editing in column mode also. $79.95 per year.
- A shell script editor for android that edits and runs shell scripts on your device. Features - Run the script from the editor. Auto indent - Syntax highlighting - Undo and Redo - Access to a.
- SQLWriter is a powerful SQL script editor for Windows System. It can: Be used for multiple database such as SQL server, Oracle, DB2, Access, My SQL. Identify the key words of SQL script.Show the objects of the database for you. SQLWriter is a powerful SQL Script Editor for Windows 98, Windows NT 4.0, Windows 2000, Windows ME, Windows XP and Window2003. It has many functions.
Here is a GIF of me using the command line to explore a directory of Shakespearean plays, counting the number of words and the number of times the word 'murder' appears, both in King Lear and in all of Shakespeare's plays:
Using the command-line interface in this interactive fashion is fine, when trying things out. However, as you've likely noticed, typing is a very error-prone activity. So for complex tasks that we want to repeat, the best practice is not to retype their code from the beginning, but to create a self-contained shell script that can be run as a one-liner.
Our first shell script
Let's start off with something easy. Make a junk directory somewhere, such as /tmp/my-playground
and change into it – we don't need to litter our actual workspace with test code.
A shell script is nothing more than a text file, which should make sense, as all of our command-line scripting has so far been, well, text.
Let's use the nano text editor to create a shell script named hello.sh
. Follow these steps:
- Run
nano hello.sh
nano should open up and present an empty file for you to work in. Type in the shell command:
- Then press Ctrl-X on your keyboard to Exit nano
- nano will ask you if you want to save the modified file. Hit the y key (for 'yes').
- nano will then confirm if you want to save to the file named
hello.sh
. Hit Enter to confirm this. Run the
hello.sh
script with this command:
Here's what the steps look like as a GIF:
So hello.sh
is not very impressive, but it captures the gist of what we want to do: wrap up a series of commands into a file, i.e. a script, so that we can re-run that script as much as we'd like.
We not only eliminate the prospect of typographic errors that occur when retyping commands, we also open up the door to making the script reusable in different contexts.
A re-usable shell script with arguments
Let's make hello.sh
more complicated. Instead of just echoing Hello world
, let's design the script so that it will say Hello
to a specific value (i.e. someone's name). And we'll make the script sound more excited.
Here's the proposed usage:
And here's a GIF:
First of all, how do we customize echo 'Hello world'
? By replacing world
with a variable. Let's try it interactively from the command-line:
Output:
A Kernel is at the nucleus of a computer. It makes the communication between the hardware and software possible. While the Kernel is the innermost part of an operating system, a shell is the outermost one.
A shell in a Linux operating system takes input from you in the form of commands, processes it, and then gives an output. It is the interface through which a user works on the programs, commands, and scripts. A shell is accessed by a terminal which runs it.
When you run the terminal, the Shell issues a command prompt (usually $), where you can type your input, which is then executed when you hit the Enter key. The output or the result is thereafter displayed on the terminal.
The Shell wraps around the delicate interior of an Operating system protecting it from accidental damage. Hence the name Shell.
When we start to learn the command-line interface, we generally learn and explore it interactively. That is, we enter one command at a time so that we can see the results of each command.
- Can split the editing windows. Compares 2 text files. $49.99-UltraEdit: HTML,PHP CSS C SAS code PL/SQL UNIX Shell Scripts Visual Basic: Windows,Linux, Mac OS: Integrated SSH, FTP, and Telnet. Multi-caret editing. Support editing in column mode also. $79.95 per year.
- A shell script editor for android that edits and runs shell scripts on your device. Features - Run the script from the editor. Auto indent - Syntax highlighting - Undo and Redo - Access to a.
- SQLWriter is a powerful SQL script editor for Windows System. It can: Be used for multiple database such as SQL server, Oracle, DB2, Access, My SQL. Identify the key words of SQL script.Show the objects of the database for you. SQLWriter is a powerful SQL Script Editor for Windows 98, Windows NT 4.0, Windows 2000, Windows ME, Windows XP and Window2003. It has many functions.
Here is a GIF of me using the command line to explore a directory of Shakespearean plays, counting the number of words and the number of times the word 'murder' appears, both in King Lear and in all of Shakespeare's plays:
Using the command-line interface in this interactive fashion is fine, when trying things out. However, as you've likely noticed, typing is a very error-prone activity. So for complex tasks that we want to repeat, the best practice is not to retype their code from the beginning, but to create a self-contained shell script that can be run as a one-liner.
Our first shell script
Let's start off with something easy. Make a junk directory somewhere, such as /tmp/my-playground
and change into it – we don't need to litter our actual workspace with test code.
A shell script is nothing more than a text file, which should make sense, as all of our command-line scripting has so far been, well, text.
Let's use the nano text editor to create a shell script named hello.sh
. Follow these steps:
- Run
nano hello.sh
nano should open up and present an empty file for you to work in. Type in the shell command:
- Then press Ctrl-X on your keyboard to Exit nano
- nano will ask you if you want to save the modified file. Hit the y key (for 'yes').
- nano will then confirm if you want to save to the file named
hello.sh
. Hit Enter to confirm this. Run the
hello.sh
script with this command:
Here's what the steps look like as a GIF:
So hello.sh
is not very impressive, but it captures the gist of what we want to do: wrap up a series of commands into a file, i.e. a script, so that we can re-run that script as much as we'd like.
We not only eliminate the prospect of typographic errors that occur when retyping commands, we also open up the door to making the script reusable in different contexts.
A re-usable shell script with arguments
Let's make hello.sh
more complicated. Instead of just echoing Hello world
, let's design the script so that it will say Hello
to a specific value (i.e. someone's name). And we'll make the script sound more excited.
Here's the proposed usage:
And here's a GIF:
First of all, how do we customize echo 'Hello world'
? By replacing world
with a variable. Let's try it interactively from the command-line:
Output:
So the question is: how do we get the hello.sh
script to read in the argument (in this case, someone's name) that we pass into it?
Windows 10 Shell Script Tutorial
We do this via a special variable in bash. The variables, $1
, $2
, $3
(and so on), refer to the first, second, and third arguments that were passed into the script from the command line.
Thus, in the above example, George
will be stored in the variable $1
when hello.sh
begins running.
So reopen hello.sh
with nano
and change the code to this:
Save the changes and run bash hello.sh Mary
to see how the output changes.
Want to return the output in all caps? Then modify hello.sh
like so, piping the output through tr to replace lowercase letters with uppercase letters:
And if you want to be concise, you've probably noticed that the variable, $yourname
, isn't really needed. The code could be simplified to this:
Conclusion
Time to slow down. If you can make a script that you can execute like this:
Then you have already learn a major concept. At the very least, you have learned the method by which programmers stuff a bunch of complicated into a 'container' that anyone else can run in a single line.
Windows Shell Script Tutorial
In the next tutorial in this series, I've taken this same idea except to go more indepth in making a script 'reusable'. Do you need to make a reusable script, right now? Do you even know what 'reusable' means? If not, then you don't need to read that tutorial, just yet.