search
HomeJavajavaTutorialA brief introduction to shell programming

A brief introduction to shell programming

Jun 26, 2017 am 11:20 AM
shellIntroductionprogramming

Shell itself is a program written in C language. It is a bridge for users to use Unix/Linux. Most of the user's work is completed through Shell. Shell is both a command language and a programming language. As a command language, it interactively interprets and executes commands entered by the user; as a programming language, it defines various variables and parameters, and provides many control structures found only in high-level languages, including loops and branches.

Although it is not part of the Unix/Linux system kernel, it calls most of the functions of the system core to execute programs, create files, and coordinate the running of various programs in a parallel manner. Therefore, for users, the shell is the most important utility program. In-depth understanding and proficiency in the characteristics and usage of the shell are the keys to making good use of the Unix/Linux system.

It can be said that the proficiency of using shell reflects the user's proficiency in using Unix/Linux.

Shell has two ways to execute commands:

  • Interactive (Interactive): interprets and executes the user's command, the user enters a command, Shell will interpret and execute one.

  • Batch processing (Batch): The user writes a Shell script (Script) in advance, which contains many commands, so that the Shell can execute these commands at once without having to type the commands one by one. .


Shell scripts are very similar to programming languages. They also have variables and flow control statements, but Shell scripts are interpreted and executed and do not need to be compiled. The Shell program reads line by line from the script. Fetching and executing these commands is equivalent to a user typing the commands in the script line by line into the Shell prompt for execution.

Shell beginners please note that in daily applications, it is recommended not to use the root account to run Shell. As an ordinary user, you cannot damage the system whether intentionally or unintentionally; but if you are root, it is different. Just typing a few letters may lead to catastrophic consequences.

Common shell script interpreters on Unix/Linux include bash, sh, csh, ksh, etc. It is customary to call them a kind of Shell. We often talk about how many kinds of Shell there are, but what we are actually talking about is the Shell script interpreter.

bash

Bash is the standard default shell of Linux. This tutorial is also based on bash. bash was jointly completed by Brian Fox and Chet Ramey. It is the abbreviation of BourneAgain Shell and has a total of 40 internal commands.

Linux uses it as the default shell because it has the following features:

  • You can use functions similar to doskey under DOS, use the arrow keys to browse and quickly Enter and modify commands.

  • Automatically give commands starting with a certain string by searching for matches.

  • contains its own help function. You only need to type help at the prompt to get relevant help.

sh

sh Developed by Steve Bourne, it is the abbreviation of Bourne Shell. sh is the standard default shell of Unix.

ash

ash shell was written by Kenneth Almquist. It is a small shell that takes up the least system resources in Linux. It only contains 24 internal commands, so it is very inconvenient to use.

csh

csh is a relatively large kernel of Linux. It is compiled by a total of 47 authors represented by William Joy and has a total of 52 internal commands. This shell actually points to a shell such as /bin/tcsh. In other words, csh is actually tcsh.

ksh

ksh is the abbreviation of Korn shell, written by Eric Gisin, with a total of 42 internal commands. The biggest advantage of this shell is that it is almost completely compatible with the commercial version of ksh, so you can try the performance of the commercial version without spending money to buy the commercial version.

Note: bash is the abbreviation of Bourne Again Shell, which is the standard default shell of Linux. It is based on Bourne shell and absorbs some features of C shell and Korn shell. bash is fully compatible with sh, that is to say, scripts written in sh can be executed in bash without modification.

Compiled Language

Many traditional programming languages, such as Fortran, Ada, Pascal, C, C++ and Java, are compiled languages. This type of language needs to convert the source code we wrote into object code (object code) in advance. This process is called "compilation".

When running the program, read the object code directly. Since the compiled object code is very close to the bottom layer of the computer, the execution efficiency is very high, which is the advantage of compiled languages.

However, since most compiled languages ​​operate at the bottom level and deal with bytes, integers, floating point numbers or other machine-level objects, implementing a simple function often requires a large amount of complex code. For example, in C++, it is difficult to perform simple operations such as "copy all files in one directory to another directory".

Interpreted Language

Interpreted language is also called "scripting language". When executing this type of program, the interpreter needs to read the source code we wrote and convert it into object code, which is then run by the computer. Because each time the program is executed, there is an extra compilation process, so the efficiency is reduced.

The advantage of using scripting programming languages ​​is that they mostly run at a higher level than compiled languages ​​and can easily handle objects such as files and directories; the disadvantage is that they are usually not as efficient as compiled languages. However, the trade-off is that it is usually worthwhile to use script programming: a simple script that takes an hour to write may take two days to implement the same function in C or C++, and generally speaking, the script execution speed is fast enough. , fast enough for people to ignore its performance problems. Examples of scripting programming languages ​​include awk, Perl, Python, Ruby, and Shell.

To use Shell scripts is based on:

  • Simplicity: Shell is a high-level language; through it, you can express complex operations concisely.

  • Portability: Using the functions defined by POSIX, the script can be executed on different systems without modification.

  • Easy to develop: a powerful and useful script can be completed in a short time.

The above is the detailed content of A brief introduction to shell programming. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
JVM performance vs other languagesJVM performance vs other languagesMay 14, 2025 am 12:16 AM

JVM'sperformanceiscompetitivewithotherruntimes,offeringabalanceofspeed,safety,andproductivity.1)JVMusesJITcompilationfordynamicoptimizations.2)C offersnativeperformancebutlacksJVM'ssafetyfeatures.3)Pythonisslowerbuteasiertouse.4)JavaScript'sJITisles

Java Platform Independence: Examples of useJava Platform Independence: Examples of useMay 14, 2025 am 12:14 AM

JavaachievesplatformindependencethroughtheJavaVirtualMachine(JVM),allowingcodetorunonanyplatformwithaJVM.1)Codeiscompiledintobytecode,notmachine-specificcode.2)BytecodeisinterpretedbytheJVM,enablingcross-platformexecution.3)Developersshouldtestacross

JVM Architecture: A Deep Dive into the Java Virtual MachineJVM Architecture: A Deep Dive into the Java Virtual MachineMay 14, 2025 am 12:12 AM

TheJVMisanabstractcomputingmachinecrucialforrunningJavaprogramsduetoitsplatform-independentarchitecture.Itincludes:1)ClassLoaderforloadingclasses,2)RuntimeDataAreafordatastorage,3)ExecutionEnginewithInterpreter,JITCompiler,andGarbageCollectorforbytec

JVM: Is JVM related to the OS?JVM: Is JVM related to the OS?May 14, 2025 am 12:11 AM

JVMhasacloserelationshipwiththeOSasittranslatesJavabytecodeintomachine-specificinstructions,managesmemory,andhandlesgarbagecollection.ThisrelationshipallowsJavatorunonvariousOSenvironments,butitalsopresentschallengeslikedifferentJVMbehaviorsandOS-spe

Java: Write Once, Run Anywhere (WORA) - A Deep Dive into Platform IndependenceJava: Write Once, Run Anywhere (WORA) - A Deep Dive into Platform IndependenceMay 14, 2025 am 12:05 AM

Java implementation "write once, run everywhere" is compiled into bytecode and run on a Java virtual machine (JVM). 1) Write Java code and compile it into bytecode. 2) Bytecode runs on any platform with JVM installed. 3) Use Java native interface (JNI) to handle platform-specific functions. Despite challenges such as JVM consistency and the use of platform-specific libraries, WORA greatly improves development efficiency and deployment flexibility.

Java Platform Independence: Compatibility with different OSJava Platform Independence: Compatibility with different OSMay 13, 2025 am 12:11 AM

JavaachievesplatformindependencethroughtheJavaVirtualMachine(JVM),allowingcodetorunondifferentoperatingsystemswithoutmodification.TheJVMcompilesJavacodeintoplatform-independentbytecode,whichittheninterpretsandexecutesonthespecificOS,abstractingawayOS

What features make java still powerfulWhat features make java still powerfulMay 13, 2025 am 12:05 AM

Javaispowerfulduetoitsplatformindependence,object-orientednature,richstandardlibrary,performancecapabilities,andstrongsecurityfeatures.1)PlatformindependenceallowsapplicationstorunonanydevicesupportingJava.2)Object-orientedprogrammingpromotesmodulara

Top Java Features: A Comprehensive Guide for DevelopersTop Java Features: A Comprehensive Guide for DevelopersMay 13, 2025 am 12:04 AM

The top Java functions include: 1) object-oriented programming, supporting polymorphism, improving code flexibility and maintainability; 2) exception handling mechanism, improving code robustness through try-catch-finally blocks; 3) garbage collection, simplifying memory management; 4) generics, enhancing type safety; 5) ambda expressions and functional programming to make the code more concise and expressive; 6) rich standard libraries, providing optimized data structures and algorithms.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.