1

I am currently debugging some prewritten csh scripts to make them executable. I am running into some issues when it comes to combining two strings to create a complete file path.

I need to combine the file path:

rdir = ../picks/rec_files/REC_FILE_

with a dynamic file name:

mcs = 68320
nm = ${mcs}.txt (for example)

When trying to combine these two strings the output has large amounts of white space between the variables that I am unsure how to get rid of. Any help would be greatly appreciated.

What I've tried:

  1. file = ${rdir}${nm}

output = ../picks/rec_files/REC_FILE_ 68320.txt

  1. file = echo ${rdir}${nm}

output = ../picks/rec_files/REC_FILE_ 68320.txt

  1. file = "${rdir}${nm}"

output = ../picks/rec_files/REC_FILE_ 68320.txt

  1. file = echo `${rdir}${nm}`

output = ../picks/rec_files/REC_FILE_ 68320.txt

What I was expecting:
output = ../picks/rec_files/REC_FILE_68320.txt

Ed Morton
  • 188,023
  • 17
  • 78
  • 185
Emma
  • 13
  • 3
  • After positing this I realized stack did not keep the spaces I put in the example outputs. There is about 3 tabs worth of space between the combined strings. Hope this clarifies any confusion about what I'm asking. – Emma Aug 15 '23 at 17:13
  • 1
    You can (and should) use [markdown formatting](https://stackoverflow.com/help/formatting) to make those whitespace characters visible. – pmf Aug 15 '23 at 17:17
  • 3
    You need to show _real_ shell (bash?) syntax, not some kind of pseudo code! `file = ${rdir}${nm}` is invalid syntax (well, not really; but it does not assign a variable, but calls the program `file` with at least one parameter). You cannot use `echo` the way you've shown to assign variables. Please [edit] the question to include _real_ assignments (copy them from your script). – knittl Aug 15 '23 at 17:29
  • 1
    Please provide a [mre] of your problem. As is, we can't really help you. The only thing I can say is that your example of variable affectation aren't bash anyway (`rdir = ../something` isn't a bash command. Or, more accurately, it is: it is command `rdir` called with argument `=` and argument `../something`, which is obviously not what you want. And, unless you do have a `rdir` command, that should just fail). EDIT: crossed message with @knittl. But at least, you see that there is some unconcerted unanimity about that... :D – chrslg Aug 15 '23 at 17:30
  • @pmf thanks for the tips. I just joined stack and was unsure about how to use all the features so I appreciate the help. – Emma Aug 15 '23 at 17:32
  • 3
    The title states "Bash", the tags say [tag:bash], but the content contains "csh". Which shell is it? – knittl Aug 15 '23 at 17:37
  • @knittl The `csh` tag was also there but has been removed by others. – pmf Aug 15 '23 at 17:43
  • `csh` has a confusing mix of assigment syntaxs, depending if you want a "local" shell variable `set locvar = "value with spaces"` or an environment variable (visible to child processes) : `setenv envvar "other value with spaces"`. (local variable is my take on this, and may offend purists! Warning!) (-;! Good luck. – shellter Aug 15 '23 at 19:35

2 Answers2

1

Not sure what you are doing as the formatting is wrong, but you should do this

bash% rdir='../picks/rec_files/REC_FILE_'
bash% mcs='68320'; nm="${mcs}.txt"
bash% file="$rdir/$nm"
bash% echo "'$file'"
'../picks/rec_files/REC_FILE_/68320.txt'

and you can verify there's no extra spaces.

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
  • @Diego Torres Milano Thank you for the comment! After lots of trial and error I found a couple trailing spaces in some random spots in my line. Thank you for drawing my attention to that possible issue <3 – Emma Aug 15 '23 at 21:14
-1

A sure way regardless of the shell variant you are using would be to pipe the concaneted variables and sanitize them from the spaces with stream editor, as in this post.

The expression would give echo "${rdir}${nm}" | sed 's/ //g'

Rich
  • 6,470
  • 15
  • 32
  • 53
josephkly
  • 44
  • 6