Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
In bash shell how do we assign extracted substring into variable without 'bad substitution' errors, please?
For example with the codes below:
fruit="apple"
echo $fruit | cut -c1-3
variable=$"{echo $fruit | cut -c1-3}"
The second line returns 'app' but the third line can't really work without bad substitution error.
Replace
variable=$"{echo $fruit | cut -c1-3}"
with
variable=$(echo $fruit | cut -c1-3)
do this instead
$ fruit=apple; var="${fruit:0:3}"; echo "$var"
app
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
In my mac terminal I am trying to store the output of a command into a variable like this
$ animals=$(cat animals.txt | head -n $plusOne | tail -n $numberofanimals | sort -u | sed -E "s/[*#/#2]//g; s/D/d/g ; s/G/g/g ; s/E/e/g ; s/H/h/g ; s/O/o/g ; s/C/c/g ; s/[!]//gā)
>
but it does not work. When I press Enter I get a > prompt on the next line. Any idea why?
You've got a smart quote ā at the end of the line instead of a regular double quote ".
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I want to write a line mac .bash_profile file while executing a shell script.I have tried below way but it gives error as
sed -i 'export PATH='$PWD:'$PATH' $HOME/.bash_profile
error
sed: 1: "/Users/dhiraj/.bash_pro ...": extra characters at the end of d command
Kindly let me know how do i resolve it ?
If you just want to append a line to your .bash_profile, what about
echo "PATH=\"$PWD:$PATH\"" >> $HOME/.bash_profile
?
And, if you want to be sure to insert only once:
grep -q -x -F 'PATH=\"$PWD:$PATH\"' $HOME/.bash_profile || echo "PATH=\"$PWD:$PATH\"" >> $HOME/.bash_profile
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm trying to run this code in bash but I can't. Bash doesn't run it! it's like it's waiting for me to continue and it doesn't think that my command is done! But I'm already done! I know that the problem is with read <<<. so how can I fix it?
Here is my code:
for f in `ls *| head -100`; do a=$(file "$f" | grep -Po ", \K\d+x\d+"`);
> IFS="x";
> read x y <<< "$a";
> done;
>
>
You have a superfluous backtick:
for f in `ls *| head -100`; do a=$(file "$f" | grep -Po ", \K\d+x\d+"`);
# Here ---^
IFS="x";
read x y <<< "$a";
done;
ShellCheck helpfully points this out. Voting to close as a simple typographical error.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am just wondering (and I'm stumped), why the if statement isn't working as it should. Have I missed something simple?
#!/bin/bash
echo -e "\nFinding the thing things on the thing, presently.\n"
DEVICE=$1
IPADDRESS=$(/sbin/ifconfig $DEVICE | grep -A 0 'inet addr:' | cut -c21-29)
for i in {1..255}
do
HOST=$(ping -c 1 $IPADDRESS.$i | grep -A 0 'from' | cut -c15-25)
if ["$HOST" != ""]
then
echo "$HOST is ALIVE!!!"
fi
done
You need to add spaces between:
if [ "$HOST" != "" ]
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I keep getting a "command not found" error after checking a variable for the substring .txt.
Here's a simple version of my script.
myscript.sh
#!/bin/sh
if [["$1" == *.txt]]
then
echo $1
fi
Result:
> ./myscript.sh argument.txt
./myscript.sh: line 2: [[argument.txt: command not found]]
The error is because of a space needed around the brackets [[ and ]]:
#!/bin/sh
if [[ "$1" == *.txt ]]
then
echo $1
fi
That is, instead of:
if [["$1" == *.txt]]
use
if [[ "$1" == *.txt ]]