B
B
by_EL2020-11-20 01:43:51
linux
by_EL, 2020-11-20 01:43:51

What does the $ symbol mean before a command in a Linux script?

Please explain exactly what the $ symbol means before the Linux command, what is it used for? PS (please just don't write that # this is under root and $ is not privileged mode, this I also know I'm not talking about this) thanks in advance

Answer the question

In order to leave comments, you need to log in

4 answer(s)
F
FanatPHP, 2020-11-20
@by_EL

In general, I got an explanation from the author of what he needs

for example, when using the value of a variable, for example, good=bad, then by executing the command echo $good

What you are talking about is not called a command, but a variable . This baffled everyone.
The explanation is very simple.
Because bash has a VERY loose syntax, and any text is treated as a string without any quotes, we need a way to distinguish a variable from just a string. If you write
good=bad
echo good

then how will the system understand WHAT you want to display - the string good or the variable good?
Therefore, there are no options here - the variable must be somehow distinguished from the rest of the text. This is what the dollar sign does.
At the same time, the dollar sign is not needed to assign a value to a variable, since the system sees by the = sign that it is an assignment (and apparently therefore you cannot put spaces around the = sign).
Yes, it's illogical. But not everything in our life lends itself to logic.
5fb765391d91b496225527.jpeg

S
Saboteur, 2020-11-20
@saboteur_kiev

It's just a prompt string.
It also means that you work under the normal user, and # - under a rue. This is its main purpose, there are no additional functions in this symbol.
The PS1 variable actually contains the prompt string, which usually contains the username, hostname, current directory, and this character.

M
mayton2019, 2020-11-20
@mayton2019

This is a variable that stores the error code of the previous command. Usually 0 is ok. And another number indicates failure.

C
CityCat4, 2020-11-20
@CityCat4

Hm. Perhaps it was possible to ask a more illiterate question, if you try. Russian is not happy, huh?
- in front of the command in the terminal - just an invitation character, means that the terminal is ready to accept the command. It is set by the PS1 environment variable, into which you can drive a rather spreading construction - for example:

mch=`hostname -s`
PS1='\[\033[01;32m\]${mch:q}:\[\033[01;34m\][$USER]\[\033[01;36m\] \!>\[\033[00;38m\] '

- in the script - $$ - contains the PID of the script, and the $ itself is just a character with which the variable starts in case you need to take its value
man bash
info bash

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question