Echo "$$" as text [duplicate] - bash

This question already has answers here:
Escape dollar sign in string by shell script
(6 answers)
Closed 5 years ago.
How I can print $$ as text. For example script:
PASS="Pa$$w0Rd"
echo $PASS
This structure show me Pa12515w0Rd where 12515 it's current PID but I need to see Pa$$w0Rd.
Thanks in advance.
For more detail:
echo "Enter password:" #pass will be - Pa$$w0Rd
read PASS
echo $PASS

You have to add an escape char
like that :
$var = "PA\$\$ss";
echo $var;
Displaying Pa$$w0rd with echo can be achieved with:
echo Pa\$\$w0rd

Related

what is wrong with my shell script for word guessing game? [duplicate]

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Closed 3 years ago.
I have written a script for word guessing in bash.
But is not working properly.
w = "###"
read -p "input word: " var
while [ "$w" != "$var" ]
do
echo "Wrong."
read -p "input word: " var
done
echo "Right answer"
You shouldn't have spaces when declaring varaible:
code="banana"

How do I print all the elements in my bash array? [duplicate]

This question already has answers here:
How to print a bash array on the same line
(6 answers)
Closed 3 years ago.
I am running this script in bash to add a list of user to an array and then write it to a file. This is the script:
echo "Insert the first user of the list"
read -r user_name
user_list=()
echo "User $user_name inserted!"
user_list+=($user_name)
echo "Do you want to insert another user?(yes or no)"
read -r answ
while [ $answ == "yes" ]; do
echo "Enter a new user to insert"
read -r new_user
user_list+=($new_user)
echo "Users list contains:"
echo "$user_list"
echo "Do you want to add another user?(yes or no)"
read -r answ
done
echo "$user_list" > user_list.txt;
Everything works fine except that the array only contains the first element.I don't understand why the $new_user are not added to the array.
Last line could be
printf "%s\n" "${user_list[#]}" >user_list.txt

Why does if condition give error in bash? [duplicate]

This question already has answers here:
Getting "command not found" error while comparing two strings in Bash
(4 answers)
Closed 6 years ago.
My code below:
echo "====================================="
echo " Test Programme "
echo "====================================="
echo
read -p "Enter Name: " name
if [$name -eq ""]; then
sleep 1
echo "Oh Great! You haven't entered name."
exit
fi
read -p "Enter age: " age
According to that code,I expected "Oh Great! You haven't entered name." to show up when user skips entering the name which WORKS WELL
But, when you enter a proper string for name, it gives this message/ error:
./cool_ham.sh: line 13: [Franco: command not found
I want to know the reason for that.
I have even tried "$name" = "" after #Jack suggested, but still din't work .
Put a space between the square braces of your if condition and its contents.
if [ "$name" = "" ]; then
Additionally note that I use = over -eq to compare strings. -eq is used to compare integer values, while = will compare strings, which can be unintuitive coming from other languages.
I also quoted $name to prevent globbing and word splitting.

How to write a multiline string to a file in Bash [duplicate]

This question already has answers here:
I just assigned a variable, but echo $variable shows something else
(7 answers)
Closed 6 years ago.
I want to rewrite a configuration file when asked from a bash script. Here is my code.
function quality {
echo $1 > ~/.livestreamerrc
echo ".livestreamer was modified!"
}
best="stream-types=hls
hls-segment-threads=4
default-stream=best
player=vlc --cache 5000"
read -p "Set quality: " INPUT
if [[ "$INPUT" == "!best" ]]; then
quality $best
fi
This code does the following to .livestreamer file though.
$cat ~/.livestreamerrc
stream-types=hls
Why?
Change it to
quality "$best" # double quotes to avoid word splitting
and then
echo "$1" > ~/.livestreamerrc
Note : Worth checking the [ shellcheck ] documentation.Also, fully uppercase variables like INPUT are reserved for the system.

bash script if and while conditions [duplicate]

This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 6 years ago.
I am having trouble with executing the following bash script:
#!/bin/bash
response=" "
while ["$response" != "q"]; do
echo -n "Please enter a response"; read response
done
ALSO
!/bin/bash
response="x"
if ["$response" = "x"]
then
echo "the value is x"
fi
What could the possible errors be?
Your while and if statements are spaced wrong.
while [ "$response" != "q" ]; do etc
You need a space between the bracket and the double quote.

Resources