1
0
Fork 0
mirror of https://codeberg.org/Mo8it/How_To_Linux.git synced 2024-10-18 11:52:39 +00:00

Explain variables

This commit is contained in:
Mo 2022-09-26 19:43:10 +02:00
parent 193eb13d05
commit 58765bc5cf

View file

@ -16,13 +16,17 @@ echo "1. Linux"
echo "2. Windows"
echo "3. Mac"
RIGHT_ANSWER=1
# The option -n does not print a new line at the end
echo -n "Enter a number: "
read ANSWER
if [ "$ANSWER" == "1" ]
if [ $ANSWER == $RIGHT_ANSWER ]
then
echo "Good choice!"
else
# Any answer other than 1
echo "Nah, that can't be right! It must be an error!" 1>&2
fi
```
@ -81,15 +85,114 @@ We could have written the Python script without the shebang, but then we would h
You can use the shebang with any program that runs a script (interpreter).
<!-- TODO: Variables -->
## Variables
<!-- TODO: $ -->
In our first bash script, we have the line `RIGHT_ANSWER=1`. This line defines a variable with the name `RIGHT_ANSWER` and the value `1`.
<!-- TODO: Escaping '$' -->
To define a variable in bash, you have to write the name of the variable _directly_ followed by an equal sign `=`. The equal sign has to be _directly_ followed by the value of the variable.
<!-- TODO: ${} -->
_directly followed_ means that spaces between the variable name, the equal sign `=` and the value are not allowed!
<!-- TODO: $() -->
But what if you want to set a variable equal to a string that contains spaces?
In this case, you have to use quotation marks `"`. For example:
```bash
HELLO="Hello world!"
```
To use a defined variable, we use a dollar sign `$` before the variable name. For example:
```bash
echo $HELLO
```
The line above would output `Hello world!`.
You can use defined variable inside a variable definition:
```bash
MESSAGE="Tux says: $HELLo"
echo $MESSAGE
```
The two lines above would lead to the output `Tux says: Hello world!`.
## Capturing command's output
You can capture the standard output of a command using the the syntax `$(COMMAND)`. Example:
```bash
BASH_VERSION=$(bash --version)
```
The line above saves the output of the command `bash --version` in the variable `BASH_VERSION`.
Lets run the command in the terminal first to see its output:
```console
$ bash --version
GNU bash, version 5.1.16(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
```
Now lets output the variable that we did define above:
```console
$ echo $BASH_VERSION
GNU bash, version 5.1.16(1)-release (x86_64-redhat-linux-gnu) Copyright (C) 2020 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software; you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.
```
You can see that the lines are squashed into one line! If you want to output the lines without them beeing squashed, you have to use quotation marks `"`:
```console
$ echo "$BASH_VERSION"
GNU bash, version 5.1.16(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
```
This is the output that we did expect 😃
## Temporary variables
Lets write the following tiny script `hello.sh`:
```bash
#!/usr/bin/bash
echo "Hello $USER!"
```
Now we run the script. The output on my machine is:
```console
$ chmod u+x hello.sh
$ ./hello.sh
Hello mo!
```
We see `Hello mo!` as output. This is because my user name on my machine is `mo`. `USER` is a so called environment variable that is defined for all programs. If you run the script on your machine, you will get your user name instead of `mo`. There are more environment variables like `PATH` which we will learn about later.
Now lets run the following:
```console
$ USER=Tux ./hello.sh
Hello Tux!
```
We did define a temporary variable `USER` with the value `Tux`. This temporary variable overwrites the environment variable `USER` in our script. Therefore we get the output `Hello Tux!` and not our user name.
You can use temporary variables not only to overwrite environment variables. These are basically variables that can be used by the program that you specify after their definition.
<!-- Comments -->
<!-- TODO: read -->
@ -115,6 +218,8 @@ You can use the shebang with any program that runs a script (interpreter).
<!-- TODO: Long command on multiple lines \ -->
<!-- Why ./SCRIPT_NANEM -->
<!-- TODO: Permissions -->
<!-- TODO: r,w,x -->