I've seen a few of these questions, and a few answers, but they only tells me how to echo the results to the screen, and often only refers to one character. I need to remove the chars ', and keep the answer inside a variable

My $var3 is: '/scripts/test1.sh', 'Alf Complete', '-', 'S1E1', '-', 'Episode 1', 'has', 'been', 'paused', 'hellfire', 'i', 'Engsub - Series', 'testing'] (It's an output from another program)

This is suppose to be converted to a command, and started by using "$var4" (and should look something like this) /scripts/test1.sh Alf Complete - S1S1 - Episode 1...

So the question is, how can I remove the chars I don't want and keep the new data in a new variable (for example $var4)

I might add that I'm very new at both Linux and writing script.. so please, treat me as "stupid"..

Edit 27/1

I'm starting the script with:

./test1.sh 'test1','test2','test3'

The text behind the script is arguments left by the program that calls the script.

This is my current script

# populate var0
read -r var0<<-EOF
messer  # is the program that is going to be called in the end of this script

# populate var1
read -r var1<<-EOF
 --command='m "Adam Larsson"   # this is the first arguments 
# populate argumentsreal
read -r argumentsreal<<-EOF
$@   #this is the arguments that are posted by the application that calls this script

var4="'"  # this adds the last closing '  to the code

# Join Forces
var3="$var0 $var1 $argumentsreal $var4"   
echo $var3>txt.txt # dumping result (bugcheck)
$var3  # Executing command

This is my code so far, my biggest issue is that when I'm running it, the program messer says Invalid message - check your syntax But if I do cat txt.txt I will get the result:

messer --command='m "Adam Larsson" testing test1,test2,test3

If I now copy that text line, and paste it on the command line, and press enter, it works perfectly.

So, why isn't it working when I'm running it from the script?

  • Where does the contents of that variable come from? If it's a command and a filename, it would be better to keep them in an array. See e.g. How can we run a command stored in a variable? – Kusalananda Jan 22 '20 at 21:12
  • The output is from Plex media server, I'm trying to get it to send a message via messenger instead of constantly sending email.. – JoBe Jan 22 '20 at 21:37
  • That link is like Russian to me.. If got the program to start, so that's not the issue, it's all the ',' that are breaking the program.. – JoBe Jan 22 '20 at 21:38
  • 1
    How will your script tell "Episode 1" from "Episode" "1", then? – RudiC Jan 23 '20 at 9:08
  • @RudiC (didn't see your comment until now) I have no idea, but this script is only meant to send the information further but without all the '',' chars.. – JoBe Jan 27 '20 at 5:52

try with tr -d \',; like printf '%s\n' "$var3" |tr -d \',; this removes all commas and single quotes from the $var variable.


Try bash's "Parameter Expansion: Pattern substitution" :

$ echo "${var3//,}"
'/scripts/tautullimess.sh' 'Alf Complete' '-' 'S1E1' '-' 'Episode 1' 'has' 'been' 'paused' 'hellfire' 'i' 'Engsub - Series''testing']
$ echo "${var3//[\',]}"
/scripts/tautullimess.sh Alf Complete - S1E1 - Episode 1 has been paused hellfire i Engsub - Series testing]
  • Anyway to echo the result so it will be executed as a command? Or echo it to another variable? – JoBe Jan 27 '20 at 7:51
  • Try $ ${var3//[\',]} – RudiC Jan 27 '20 at 7:55
  • Yeah, oh, does that executes it as a command, I thought it only displayed the result (newbie as I wrote) – JoBe Jan 27 '20 at 7:57
  • The result was: ./test1.sh: 22: ./test1.sh: Bad substitution and I exexuted: ./test1.sh 'kalle','damke','hekl' one interesting thing is that when I just echoed $var3 without your modification it seemed to strip the ' signs by it self: messer --command='m "Adam Larsson" testing kalle,damke,hekl ' – JoBe Jan 27 '20 at 8:09
  • More context necessary. For me, it works (prints param count, and parameters): ${var3//[\',]} - CNT: 16, P1: Alf, P2: Complete, ALL: Alf Complete - S1E1 - Episode 1 has been paused hellfire i Engsub - Series testing] – RudiC Jan 27 '20 at 8:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.