-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

  • 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 – openInvent Nov 09 '18 at 02:46
  • Using an alias is bad practice whether or not it is recursive. – William Pursell Nov 09 '18 at 02: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. – William Pursell Nov 09 '18 at 02:55
  • I wasn't using quotes and that was THE PROBLEM I guess. – openInvent Nov 09 '18 at 03: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. – tripleee Nov 09 '18 at 05:21

1 Answers1

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}"'/'
perreal
  • 94,503
  • 21
  • 155
  • 181