YouTip LogoYouTip

Linux Shell

# Shell Tutorial Shell is a program written in C language, serving as a bridge for users to interact with Linux. Shell is both a command language and a programming language. Shell refers to an application that provides an interface through which users can access the services of the operating system kernel. Ken Thompson's sh was the first Unix Shell, while Windows Explorer is a typical graphical shell. [**Shell Online Tool**](#) * * * ## Shell Script A shell script is a script program written for the shell. In the industry, "shell" usually refers to shell scripts, but readers should be aware that shell and shell script are two different concepts. Due to habit and for brevity, "shell programming" mentioned in this article refers to shell script programming, not the development of the shell itself. * * * ## Shell Environment Shell programming is similar to JavaScript or PHP programming; all you need is a text editor for writing code and a script interpreter for execution. There are many types of Shell in Linux, common ones include: * Bourne Shell (/usr/bin/sh or /bin/sh) * Bourne Again Shell (/bin/bash) * C Shell (/usr/bin/csh) * K Shell (/usr/bin/ksh) * Shell for Root (/sbin/sh) * ... This tutorial focuses on Bash, also known as Bourne Again Shell. Due to its ease of use and free availability, Bash is widely used in daily work. At the same time, Bash is the default Shell for most Linux systems. In general, people do not distinguish between Bourne Shell and Bourne Again Shell, so **#!/bin/sh** can also be changed to **#!/bin/bash**. #! tells the system that the program specified by the following path is the Shell program that interprets this script file. * * * ## First Shell Script Open a text editor (you can use the vi/vim command to create a file), create a new file named test.sh. The extension is sh (sh stands for shell). The extension does not affect script execution; it's just for clarity. If you write shell scripts in PHP, you can use the php extension. Enter some code. The first line is usually like this: ## Example #!/bin/bash echo"Hello World !" [Run Example Β»](#) #! is a conventional marker that tells the system which interpreter is needed to execute this script, i.e., which Shell to use. The echo command is used to output text to the window. ### There are two ways to run a Shell script: **1. As an executable program** Save the above code as test.sh and cd to the corresponding directory: chmod +x ./test.sh # Make the script executable ./test.sh # Execute the script Note that it must be written as ./test.sh, not **test.sh**. The same applies when running other binary programs. If you just write test.sh, the Linux system will look for it in the PATH. Only /bin, /sbin, /usr/bin, /usr/sbin, etc., are in the PATH. Your current directory is usually not in the PATH, so writing test.sh will result in a "command not found" error. You need to use ./test.sh to tell the system to look in the current directory. **2. As an interpreter parameter** This method involves running the interpreter directly, with the shell script filename as the parameter, such as: /bin/sh test.sh /bin/php test.php Scripts run this way do not need to specify interpreter information in the first line; it is useless even if you do.
← Linux File Content ManageLinux Networking Commands β†’