From 52aa547d15261d3dd0ccf74f576ad9c2d295e048 Mon Sep 17 00:00:00 2001 From: rohitaryal Date: Mon, 22 Jul 2024 23:38:28 +0530 Subject: [PATCH 1/2] Added special variables --- README.md | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/README.md b/README.md index 27afa2f..c59016e 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ 1.5. [Process Monitoring Operations](#15-process-monitoring-operations) 2. [Basic Shell Programming](#2-basic-shell-programming) 2.1. [Variables](#21-variables) +         2.1.1. [Special variables](#211-special-variables) 2.2. [Array](#22-array) 2.3. [String Substitution](#23-string-substitution) 2.4. [Other String Tricks](#24-other-string-tricks) @@ -994,6 +995,55 @@ Example: ```bash echo $str # hello world ``` + +## 2.1.1. Special Variables + +### a. `$!` +Expands to the PID (Process ID) of the most recent job placed into background. + +### b. `$@` +Expands to the positional parameters passed to script/function. + +Example: +```bash +#!/bin/bash +function demo { + echo $@ +} +demo foo bar # Output: foo bar +``` + +### c. `$#` +Expands to number of positional parameters passed to a script/function. + +Example: +```bash +#!/bin/bash +function param_count { + echo $# +} +param_count foo bar foo # Output: 3 +``` + +### d. `$$` +Expands to the PID (Process ID) of current running shell/shell script. In subshell it expands to PID of invonking shell, not current subshell. + +### e. `$*` +Expands to positional parameters passed to script/function, similar to `$@` but the difference is noticed when we put quotes around `$*` and `$@` +`./test.sh "$*"` - `test.sh` recieves `arg1 arg2 arg3` as single argument +`./test.sh "$@"` - `test.sh` recieves `arg1`, `arg2`, `arg3` as seperate argument + +### f. `$_` +Holds the value of the last argument to the previous command + +Example +```bash +$ echo Hello, bashers +Hello, bashers +$ echo $_ +bashers +``` + ## 2.2. Array Like other languages bash has also arrays. An array is a variable containing multiple values. There's no maximum limit on the size of array. Arrays in bash are zero based. The first element is indexed with element 0. There are several ways for creating arrays in bash which are given below. From fa8f2a4481cfadfa8f10240c4f17c2d4a59f1c96 Mon Sep 17 00:00:00 2001 From: rohitaryal Date: Mon, 22 Jul 2024 23:41:38 +0530 Subject: [PATCH 2/2] Added example for $ exclamation --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index c59016e..50558e2 100644 --- a/README.md +++ b/README.md @@ -1001,6 +1001,14 @@ echo $str # hello world ### a. `$!` Expands to the PID (Process ID) of the most recent job placed into background. +Example: +```bash +$ sleep 20 & +[1] 30969 # PID +$ echo $! +30969 +``` + ### b. `$@` Expands to the positional parameters passed to script/function.