How to fix command not found in Linux shell scripting [duplicate] - linux

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
why subtraction return - symbol
(2 answers)
Closed 2 years ago.
I have a Linux shell script with the below code
#! /bin/bash
echo $BASH
name = Mark
echo $name
When I run the script, I'm getting an error:
./my_script.sh: line 3: =: command not found
What am I doing wrong?
Note: I'm using Kali Linux.

In shell, you need to write:
echo $BASH
name=Mark
echo $name
Note there are no spaces around = when setting a variable. The shell usually interprets name = Mark as calling the command name with arguments =and Mark, hardly what you intend. It also seems that name somehow expands to nothing (an alias?), thus the confusing message about command =.

Related

syntax error `(" unexpected in bash script [duplicate]

This question already has answers here:
Difference between sh and Bash
(11 answers)
Closed 2 years ago.
i'm coding a simple bash script and i found this error syntax error at line XX `(' unexpected
my code:
function myfun(){
echo XXXX
echo YYYY
read choice
}
choice=$(myfun)
where is the error. i used the ShellCheck and no errors were detected.
Make sure you are running the script with bash. That error is a commonly seen dash shell error.
I suspect the first line of your script is not #!/bin/bash, i.e. you may have left out the shebang line entirely resulting in the default shell being used (which will often be dash especially on Debian derived Linuxes where /bin/sh -> dash).
Try running this:
#!/bin/bash
myfun()
{
echo XXXX
echo YYYY
read choice
}
choice=$(myfun)

How to fetch output of command into a variable? [duplicate]

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Closed 2 years ago.
I am trying to run a command and storing the values in a list
list = `sed -n 's/^abc//p' /etc/filename`
I am getting an error command not found while running the above command.
However, when I directly run the sed -n 's/^abc//p' /etc/filename command, the output is coming fine as below:
abc01 abc02 abc03
Use
list="$(sed -n 's/^abc//p' /etc/filename)"
There must be no spaces after variable declaration and equals sign. Also, quoting your variables is important.

Replace Tilde with $HOME in bash script [duplicate]

This question already has answers here:
Tilde expansion in quotes
(3 answers)
How to manually expand a special variable (ex: ~ tilde) in bash
(19 answers)
Closed 4 years ago.
I've been digging through the internet and I see examples of replacing $HOME with ~, but I'm trying to go the other way (e.g. - replace ~ with $HOME and currently if I try to run this:
if [[ $directory_name = *~* ]]; then
echo "${$directory_name/\~/$HOME}"
fi
to replace it, I get this error:
${$directory_name/\~/$HOME}: bad substitution
I have #!/bin/bash at the top of my script file and when I run it I've been using something like this:
sh test-script.sh
I'm also doing this in terminal on a Mac, so I'm not sure if that has anything to do with it.
Again...new to bash scripting so while this seems logical, I could be going about this all wrong and missing something. Thanks!

Space between lines in shellscript being taken as a command [duplicate]

This question already has an answer here:
Why would a correct shell script give a wrapped/truncated/corrupted error message? [duplicate]
(1 answer)
Closed 6 years ago.
I am writing a bash script which is a follows
#!/bin/bash
#getting the environment variable from commandline
environment=$1
echo $environment
Now when I run the script with bash ./bashScript.sh Hello , I get the following errors on line
: command not found line 2
: command not found line 5
I see that both of these lines are space and bash script is thus giving me an error
To solve it I write my script as
#!/bin/bash
#
#getting the environment variable from commandline
environment=$1
#
echo $environment
But it looks kind of messy
Is there any other way to achieve this. Thanks for help in advance.
Your comment add the precision that your lines use DOS end of lines (\r\n) when you dump it with od -xc file. To avoid it, you should make sure that your editor uses Unix end of lines (\n).
To fix it on an existing text file, you can use tr:
tr -d '\r' < dos_file > unix_file

Bash foreach loop works differently when executed from .sh file [duplicate]

This question already has answers here:
Difference between sh and Bash
(11 answers)
How do I iterate over a range of numbers defined by variables in Bash?
(20 answers)
Closed 9 years ago.
The following 'oneliner' does what I need:
$ for i in {1..5}; do echo $i; done
1
2
3
4
5
However, when I place exactly the same code into for-each.sh file and execute it, I get different result. Why?
for-each.sh file:
#!/bin/bash
for i in {1..10}; do echo $i; done
Result after execution:
$ ./for-each.sh
{1..10}
EDIT
Uf. I'm sorry. Now I noticed that I executed the for-each.sh by sh ./for-each.sh command and not by ./for-each.sh. I didn't know the difference between bash, sh, dash, ... After reading stackoverflow.com/a/5725402/915756 I realized that I executed the file by dash which points to /bin/sh by default on my Debian machine.
If you're confident that it is indeed bash executing your script, you can explicitly turn on brace expansion (expansion of {...} expressions) as follows:
set -B # same as: set -o braceexpand
Make this your script's first command after your shebang.
(Conversely, set +B (set +o braceexpand) would turn brace extension OFF.)
Conceivably, your system is configured to have brace extension turned off by default.

Resources