-2

UPDATE: I learned input variables can be accessed with commands like xargs getopts() and using $@ $% and $# as well as !* for the output of last command

To clarify the question when I run a command like

rep="replace" && echo foobar | sed -e 's/.*/${rep}/'

the text is not replaced with variable contents I tried wrapping in brackets quotes and percent signs. I also tried seds insert command.

Any there any methods to debug or step thru Bash? I noticed pressing ctrl Z rather than ctrl C lets me see return values.

For those who like to say "duplicate" rather than answer; These posts didnt help me:

Replace string variable with string variable using Sed

"sed" special characters handling

Other notes: //WARNING// in this command I set an alias that runs itself is that bad practice? I think I was trying codegolf code and ending up making directories with [[ in my $home directory on live disc so careful on your install.

alias a="espeak $1" && b=$($a) //the b part runs it and can be omited

for example this works fine after the alias of a seq 11 -1 0 |a

alias countDown="seq $1 -1 0|a" doesn't work with countDown 10 as an example run.

This method omits sed command: @destenson post at Escape a string for a sed replace pattern

inputvar="foobar" && txt2replace="oo" && txt2replacewith="rench" && outputvar="${inputvar//"$txt2replace"/"$txt2replacewith"}" && echo $outputvar && echo $outputvar|a

helped

5
  • does the string $rep contain escape characters how do I debug and see them? $rep|hd? shows me an extra '.' char but so does echo "replace"|hd Commented Nov 9, 2018 at 2:46
  • Using an alias is bad practice whether or not it is recursive. Commented Nov 9, 2018 at 2:53
  • Pressing ctrl-Z does something very different than ctrl-C, so the behavior is different. One suspends the process (by sending SIGTSTP), the other kills it (by sending SIGINT). You seem surprised that they do different things. Commented Nov 9, 2018 at 2:55
  • I wasn't using quotes and that was THE PROBLEM I guess. Commented Nov 9, 2018 at 3:24
  • One question per question, please. I added a bunch of duplicates but you might want to create a new question which asks a single question if you need more guidance on the debugging topic in particular. Commented Nov 9, 2018 at 5:21

1 Answer 1

0

Put the variable out of the single quotes:

rep="replace" && echo foobar | sed -e 's/.*/'${rep}'/'

Double quotes are fine:

rep="replace" && echo foobar | sed -e 's/.*/'"${rep}"'/'
Sign up to request clarification or add additional context in comments.

4 Comments

That worked but is confusing. so what happens with $rep or %$rep% and ${rep} how can I debug bash?
$(rep="echo thxM8" && echo foobar | sed -e 's/.*/'"${rep}"'/')|espeak
You can debug it like this: rep="replace" && echo "echo foobar | sed -e 's/.*/'"${rep}"/"
I meant is there a way to debug bash like by alias [="test && $(echo ${1})"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.