This question already has answers here:
How to properly nest Bash backticks
(6 answers)
Closed 3 years ago.
Is there a way to use ` inside a ` in bash script
This works
echo `echo '(!1)*2' | bc -l`
but this doesnt
echo `echo '(!`echo 1`)*2' | bc -l`
the error is
unmatched '
how do I fix this?
You can make use of $() instead of ``:
$ echo $(echo $(echo $(echo "Hello") world)! )
Or the solution to your answer:
$ echo $(echo "( ! $(echo 1) )*2" | bc -l)
p.s. don't use ' when you want to make use of variables inside a string. Only double quotes " are parsed.
Related
This question already has answers here:
How to pass the value of a variable to the standard input of a command?
(9 answers)
Closed 1 year ago.
im running this in bashrc file
function simplefunc () {
output=$(ls -1 "$HOME")
linecount=$(${output} | wc -l)
echo "${linecount}"
echo "${output}"
}
getting this error
Desktop: command not found
0
Desktop
Documents
Downloads
Music
Pictures
Public
snap
SoftMaker
Templates
venv
Videos
i tried these too
putting "local" before variable or
# linecount=$(output | wc -l)
# echo "$(${output} | wc -l)"
I think you should change the third line to:
linecount="$(echo "${output}" | wc -l)"
# OR alternatvely:
# linecount="$(wc -l < <(echo "$output"))"
This question already has answers here:
uppercase first character in a variable with bash
(17 answers)
Closed 2 years ago.
editAppsDotPy() {
echo 'from django.apps import AppConfig' >> apps.py
echo >> apps.py
echo >> apps.py
echo "class ${APP_NAME}Config(AppConfig):" >> apps.py
echo " name = '${APP_NAME}'" >> apps.py
}
How would you capitalize the variable in the 5th line?
I was trying to do it with ${APP_NAME^} but it returns me an error.
Your function rewritten to work with more various shells:
script.sh:
#!/usr/bin/env sh
capitalize()
{
printf '%s' "$1" | head -c 1 | tr [:lower:] [:upper:]
printf '%s' "$1" | tail -c '+2'
}
editAppsDotPy()
{
cat >> 'app.py' <<EOF
from django.apps import AppConfig
class $(capitalize "$APP_NAME")Config(AppConfig):
name = '$APP_NAME'
EOF
}
APP_NAME='foo'
editAppsDotPy
Demoing:
sh script.sh
cat app.py
Output:
from django.apps import AppConfig
class FooConfig(AppConfig):
name = 'foo'
Assuming that tr is in your path, the more common parameter substitutions can help you too.
Your fifth line could look like the following:
echo "class `tr [:lower:] [:upper:] <<<${APP_NAME:0:1}`${APP_NAME:1}Config(AppConfig):" >> apps.py
I also tested this in zsh 5.8.
If your version of bash is too old to support that extension (Like the OS X version), or you're using a shell like zsh that doesn't support it at all, you have to turn to something else. Personally, I like perl (Which I think OS X comes with):
$ perl -ne 'print ucfirst' <<<"foobar"
Foobar
or for something in the middle of a longer string:
$ foo=bar
$ echo "foo='$(perl -ne 'print ucfirst' <<<"$foo")'"
foo='Bar'
which works in bash and zsh.
I'm trying to execute the following bash script but it gives me invalid arithmetic operator error in line 8.
#!/bin/bash
criteria=$1
re_match=$2
replace=$3
for i in $( ls *$criteria* );
do
src=$i
tgt=$[echo $i | sed -e "s/$re_match/$replace/"]
mv $src $tgt
done
You need to do:
$(echo $i | sed -e "s/$re_match/$replace/")
instead of
$[echo $i | sed -e "s/$re_match/$replace/"]
$() is used for variable expansion. [] is used for doing arithmetic.
This question already has answers here:
unix sed substitute nth occurence misfunction?
(3 answers)
Closed 4 years ago.
In bash, suppose I have the input:
ATGTGSDTST
and I want to print:
AT
ATGT
ATGTGSDT
ATGTGSDTST
which means that I need to look for all the substrings that end with 'T' and print them.
I thought I should use sed inside a for loop, but I don't understand how to use sed correctly in this case.
Any help?
Thanks
The following script uses sed:
#!/usr/bin/env bash
pattern="ATGTGSDTST"
sub="T"
# Get number of T in $pattern:
num=$(grep -o -n "T" <<< "$pattern" | cut -d: -f1 | uniq -c | grep -o "[0-9]\+ ")
i=1
text=$(sed -n "s/T.*/T/p" <<< "$pattern")
echo $text
while [ $i -lt $num ]; do
text=$(sed -n "s/\($sub[^T]\+T\).*/\1/p" <<< "$pattern")
sub=$text
echo $text
((i++))
done
gives output:
AT
ATGT
ATGTGSDT
ATGTGSDTST
No sed needed, just use parameter expansion:
#! /bin/bash
string=ATGTGSDTST
length=${#string}
prefix=''
while (( ${#prefix} != $length )) ; do
sub=${string%%T*}
sub+=T
echo $prefix$sub
string=${string#$sub}
prefix+=$sub
done
This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 2 years ago.
How do I assign a command output to a shell script variable.
echo ${b%?} | rev | cut -d'/' -f 1 | rev
${b%?} gives me a path..for example: /home/home1
The above command gives me home1 as the output. I need to assign this output to a shell script variable.
I tried the below code
c=${b%?} |rev | cut -d '/' -f 1 | rev
echo $c
But it didn't work.
To assign output of some command to a variable you need to use command substitution :
variable=$(command)
For your case:
c=$(echo {b%?} |rev | cut -d '/' -f 1 | rev)
Just wondering why dont you try
basename ${b}
Or just
echo ${b##*/}
home1
If you want to trim last number from your path than:
b="/home/home1"
echo $b
/home/home1
b=${b//[[:digit:]]/}
c=$(echo ${b##*/})
echo ${c}
home
Just like this:
variable=`command`