How To Have The Directory Changed After A Script Is Done In Bash
Time changes, and so do variables!
You must accept played with variables quite a fleck if you did whatsoever sort of programming.
If yous never worked with variables before, you can think of them as a container that stores a piece of information that can vary over fourth dimension.
Variables always come in handy while writing a bash script and in this tutorial, you will learn how to use variables in your bash scripts.
Using variables in bash shell scripts
In the last tutorial in this series, you lot learned to write a hullo world program in bash.
#! /bin/bash echo 'Hello, World!'
That was a simple Hello World script. Let's make it a better Hello Globe.
Let'southward ameliorate this script by using shell variables then that it greets users with their names. Edit your hullo.sh script and apply read control to become input from the user:
#! /bin/fustigate echo "What's your name, stranger?" read name repeat "Howdy, $proper noun"
At present if you run your howdy.sh script; it will prompt you lot for your proper noun and then it volition greet you with whatever name yous provide to information technology:
[e-mail protected]:~/scripts$ ./howdy.sh What's your name, stranger? Elliot Hello, Elliot
In the above example, I entered Elliot equally my proper noun and so the script greeted me with "Howdy, Elliot". That's definitely much improve than a generic "Hi, World" program. Don't you agree?
Step by step explanation of the above trounce script
At present let's become over the script line by line hither to make certain that you understand everything.
I kickoff included the shebang line to explicitly state that we are going to use bash shell to run this script.
#!/bin/fustigate
Side by side, I ask the user to enter his/her name:
echo "What's your proper noun, stranger?"
That'south just a simple echo command to print a line to the last; pretty self-explanatory.
At present here'south the line where all the magic happens:
read name
Hither, I used the read command to transfer the control from running script to the user, and then that the user can enter a name and and then store whatever user entered, in the 'name' variable.
Finally, the script greets the user with their name:
repeat "Hello, $name"
Find hither, you have to precede the variable name with a dollar sign to go the value stored in the variable proper noun. If you omit the dollar sign, "Hi, name" would be displayed instead.
This dollar sign is known as the dereference operator in bash scripting.
Integers, strings or characters? How to create different variable data types in bash vanquish?
Let's mess around a little bit more with the variables.
You lot can use the equal sign to create and set the value of a variable. For example, the post-obit line will create a variable named age and will set its value to 27.
age=27
Afterwards you accept created the age variable, you tin can change its value every bit much as you want.
age=3
The above command changes the value of the variable age from 27 to 3. If just times can become back, I tin hear you lot saying!
Variables can hold different types of data; variables can store integers, strings, and characters.
letter of the alphabet='c' colour='blue' twelvemonth=2020
Constant variables in bash trounce
You can also create a constant variable, that is to say, a variable whose value will never change! This can exist done past preceding your variable proper noun with the readonly
control:
readonly PI=3.14159
The above control volition create a constant variable PI and set its value of three.14159. At present, yous can't' change the value of constant variable, if you try, you lot will get an error:
bash: PI: readonly variable
As y'all can see, you can but read the value of a constant variable, but you can never change its value later it is created.
Command substitutions
The ability to store the output of a command into a variable is chosen command substitution and it'due south by far ane of the most amazing features of fustigate.
The appointment command is a classic example to demonstrate command substitution:
TODAY=$(appointment)
The higher up control will store the output of the command appointment into the variable TODAY. Detect, how you demand to enclose the appointment command inside a pair of parentheses and a dollar sign (on the left).
Alternatively, you can besides enclose the command within a pair of dorsum quotes:
TODAY=`date`
The back quote method is the old way of doing command substitution, and so I highly recommend that you avoid it and stick with the modern approach:
variable=$(command)
Before yous go, endeavour turning Hello Globe script to a smart HelloWorld script
At present since you lot just learned how to practise command exchange, information technology would make sense to visit the How-do-you-do Earth script one last fourth dimension to perfect it!
Last time, you asked the user to enter his/her name then the script greets them; this fourth dimension, you lot are not going to ask, your script already knows it!
Utilise the whoami command along with command commutation to greet whoever run the script:
#! /bin/bash echo "Hi, $(whoami)"
Every bit you can see, you merely needed just 2 lines! Now run the script:
./hi.sh
Information technology works like a charm!
Alright, this brings united states to the stop of this tutorial. Yous may practice what you lot merely learned by solving the problems and refer to their solutions if you get stuck or demand a hint.
I hope you have enjoyed working with shell variables as much as me. Check out the adjacent affiliate in this series, where I hash out how you tin can pass arguments to your beat scripts.
Source: https://linuxhandbook.com/bash-variables/
Posted by: truesdalehimat1991.blogspot.com
0 Response to "How To Have The Directory Changed After A Script Is Done In Bash"
Post a Comment