Shell Conception
Shell Conception
A shell is a command-line interface that allows users to interact with the operating system. It provides a way to execute commands, run scripts, and manage files and processes. The shell acts as an intermediary between the user and the operating system, interpreting user input and executing the corresponding commands.
1 | cat /etc/shells |
example:1
2
3
4Check the current shell
echo $SHELL
Change the default shell to zsh
chsh -s /bin/zsh
1 | #! /bin/bash |
Shell Parameters
Shell parameters are variables that hold values and can be used in shell scripts or command-line operations. They can be categorized into different types, such as positional parameters, special parameters, and environment variables.
- Positional Parameters: These are parameters that are passed to a shell script or command in a specific order. They are accessed using the
$1,$2, etc. syntax, where$1refers to the first parameter,$2to the second, and so on. - Special Parameters: These are predefined parameters that have special meanings in the shell. For example,
$#represents the number of positional parameters,$@represents all the positional parameters as separate words, and$?represents the exit status of the last command executed. - Environment Variables: These are variables that are set in the shell environment and can be accessed by any command or script. They are typically defined using the
exportcommand and can be accessed using the$VARIABLE_NAMEsyntax.
Example:1
2
3
4
5
6!/bin/bash
This script demonstrates the use of shell parameters
echo "Number of parameters: $#"
echo "All parameters: $@"
echo "First parameter: $1"
echo "Second parameter: $2"
Shell Variables
Shell variables are used to store data that can be used and manipulated within a shell script or command-line session. They can hold various types of data, such as strings, numbers, or even the output of commands. Shell variables can be defined and accessed using the VAR_NAME=value syntax for assignment and $VAR_NAME syntax for access.
Example:1
2
3
4
5
6!/bin/bash
This script demonstrates the use of shell variables
NAME="Alice"
AGE=30
echo "Name: $NAME"
echo "Age: $AGE"
Shell Functions
Shell functions are reusable blocks of code that can be defined and called within a shell script or command-line session. They allow you to organize your code, improve readability, and avoid repetition. Shell functions are defined using the function keyword or simply by using the () syntax, and they can accept parameters and return values.
Example:1
2
3
4
5
6!/bin/bash
This script demonstrates the use of shell functions
greet() {
echo "Hello, $1!"
}
greet "Alice"
Shell Commands
Shell commands are instructions that can be executed in a shell environment. They can be built-in commands provided by the shell itself, or they can be external commands that are available in the system. Shell commands can be used to perform various tasks, such as file manipulation, process management, and system administration. Some common shell commands include ls for listing files, cd for changing directories, and echo for printing output to the console.
Example:1
2
3
4
5!/bin/bash
This script demonstrates the use of shell commands
echo "Current directory: $(pwd)"
echo "Files in the current directory:"
ls -l
Read more about shell commands in the Shell Commands section of the Bash manual.
Example Shell Script
1 | !/bin/bash |
