Shell replacement
What is the alternative (Substitution) in Unix / Linux?
Shell does the magic when it encounters an expression that contains one or more special characters.
For example:
Here is an example, while printing a value of the variable, it will be replaced by the value of the variable. At the same time "n" is replaced by a new line.
#! / bin / sh a = 10 echo - e "Value of a is $ an"
It will produce the result as shown below. Here, the -e function will allow to compile the backslash escape sequence.
Value of a is 10
Without the -e function, it produces the following result:
Value of a is 10 n
Here are the escape sequences that can be used by echo command:
Escape sequence Description Backslash a Warning sign (BEL) b Backspace key c line cross-section f form feed n New line r Return t Tab horizontal v Vertical tabYou can use the -E function to prevent translation of cross braces (default).
You can use the -n function to disallow insertion of new lines.
Command substitution (Command Substitution) in Unix / Linux
Command substitution is a technique by which Shell executes a set of provided commands and then replaces the results of the commands.
Syntax
Command substitution is performed when a command is provided as follows:
`command`
When executing a command replacement, make sure that you are using 2 single-type quotation marks (`), not" ".
For example
Command substitution is normally used to assign the output of a command to a variable. Each of the following examples explains how to use the command substitution.
#! / bin / sh DATE = `date` echo " Date is $ DATE " USERS = ` who | wc -l` echo "Logged in user are $ USERS" UP = `date; uptime` echo "Uptime is $ UP"
It will produce the following result:
Date is Thu Jul 2 03 : 59 : 57 2009 MST Logged in user are 1 Uptime is Thu Jul 2 03 : 59 : 57 2009 MST 03 : 59 : 57 up 20 days , 14 : 03 , 1 user , load avg : 0.13 , 0.07 , 0.15
Variable Substitution in Unix / Linux
Variable substitution allows Shell programmers to manipulate the value of the variable based on its state.
Below is a table for all possible alternatives:
Sample Description $ {var} Replace the value of var . $ {var: -word} If var is null or unset, word is replaced for var . The value of var does not change. $ {var: = word} If var is null or unset, var is set to the value of word . $ {var:? message} If var is null or unset, the message printed is an error. This command checks to see if the variables are set correctly. $ {var: + word} If var is set, word is replaced for var. The value of var does not change.For example:
The following is an example to illustrate the various states of the replacement above:
#! / bin / sh echo $ { var : - "Variable is not set" } echo "1 - Value of var is $ {var}" echo $ { var : = "Variable is not set" } echo "2 - Value of var is $ {var} " unset var echo $ { var : + " This is default value " } echo " 3 - Value of var is $ var " var = " Prefix " echo $ { var : + " This is default value " } echo " 4 - Value of var is $ var " echo $ { var :? "Print this message" } echo "5 - Value of var is $ {var}"
It will produce the following result:
Variable is không đặt 1 - Value of var là Variable không đặt 2 - Value of var là Variable không đặt 3 - Value of var này là mặc định 4
According to Tutorialspoint
Previous article: Loop control in Unix / Linux
Next article: Techniques cited in Unix / Linux
You should read it
May be interested
- Google engineers develop URL alternativesprepare the birth of something simpler but still reliable and hard to deceive.
- How to Get a Replacement Phone from ATTif you use at&t for your wireless service and your phone malfunctions, gets lost, or gets damaged, you may be able to get a replacement for little to no charge. if you bought your phone recently and it has a technical problem, it could be...
- What is Classic Shell? How to use Classic Shell to customize Menu on Windows 10, 8, 7classic shell is a popular start menu custom application with stability and ease of use and operation. if you're fed up with the default start menu on windows, we can use and change the wind and customize the start menu to make your desktop fresher.
- Everything you can do with the new Windows 10 Bash Shellyou can install the linux environment and the bash shell on any version of windows 10. today's article will summarize what you can do in the new windows 10 bash shell.
- Steps to fix 'Shell Infrastructure Host Has Stopped Working' error in Windowsis your windows device showing 'shell infrastructure host has stopped working' error? in most cases, this problem occurs when running the application on a windows guest account.
- Basic Shell commands in Linuxworking on the linux operating system you cannot not know the basic shell commands. for ease of operation on linux, tipsmake.com has compiled some basic linux shell commands, please consult.
- Code Python on the browser with these free online Python interpretersyou should use an interactive interactive shell. these are sites that allow you to write, execute python code and immediately see the results, without installing anything.
- What is 'Windows Shell Experience Host' and why run on the computer?have you ever seen a process called windows shell experience host in task manager window? if you have seen, do you know what this process is? and why does it sometimes 'eat' a lot of cpu and computer memory? this article will explain to you the windows shell experience host process on task manager.
- Basic Shell operatorsthere are many operators supported by each shell. our tutorial is based on the default shell (bourne) so we are discussing bourne shell operators in this chapter.
- Math functions are available in Shellmost parts of the tutorial are mostly built on bourne shell, but this page lists all the built math functions available in the korn shell.