0

I'm having issues combining two strings in bash on Linux. My end goal is simply to add a / to an existing path I have stored as a variable. Essentially I just want the path to lead into the folder instead of just to the folder itself.

The file path is fetched from an environment variable and so far everything I have tried to combine the file path with any form of string has simply failed.

At first, I assumed that it was an error caused by the / being seen as some special character so I've been testing with just normal strings but I haven't been able to make that work either.

Here's my test code:

filename=$NEMO_SCRIPT_SELECTED_FILE_PATHS

stringtest=boo

mytest="$filename$stringtest"

zenity --info --text=$mytest

I have tried also tried:

filename+="boo"
filename+="$stringtest"
filename="${filename} test"
filename="${filename} ${stringtest}"
mytest=$filename$stringtest
filename=$(printf "%s boop" "$filename")

No matter what I seem to do I only get the file path as an output. The only time I have had any success was when adding the file path to another string instead of adding a string to the file path.

Thanks for the read

EDIT: As suggested by @thatotherguy below, I was missing "" around my variable mytest in my zenity command. Adding those solved it. Thanks

underscore
  • 49
  • 1
  • 6
  • Where are you adding a / in any of that string concatenation? – Shawn Oct 13 '19 at 03:12
  • I tried all your lines of code and they all work. The only difference is I had `#!/bin/bash` in the first line of my test script. – Nic3500 Oct 13 '19 at 03:12
  • Sounds like your problem is that `$NEMO_SCRIPT_SELECTED_FILE_PATHS` has a trailing space. Since you don't quote `"$mytest"` in your zenity command, you will only ever show the first word of it. Please run the script with `bash -x` to get a debug log, and edit your post to include it. – that other guy Oct 13 '19 at 03:15
  • @Shawn As I mentioned, I tried with a / at first but since it did not work I have been testing the other methods with normal strings, my thought being that / might be a special character that could cause problems in my problem-solving. – underscore Oct 13 '19 at 03:19
  • @thatotherguy Welp... I knew it had to be something small I had missed. Adding the "" around mytest in the zenity command as you suggested solved it. Thanks! Will edit post to include it as the answer. – underscore Oct 13 '19 at 03:22
  • @underscore [shellcheck](https://www.shellcheck.net) automatically warns about this and other common issues. You can install it in your editor and have it point out these things while you're writing the script. – that other guy Oct 13 '19 at 03:54

0 Answers0