Search code examples
bashshellstring-concatenation

Appending text to the end of a variable


The following works, but I don't want the space that it returns:

read input
file= "$input"
file= "$file ins.b" # how to get rid of the space here?
echo "$file"

This outputs 'file ins.b' I don't want the space between file and ins.b

If I don't leave that space in the code it returns only '.b'. What can I do to resolve this problem?


Solution

  • Append like:

    file="${file}ins.b" 
    

    If you don't use braces then it treats fileins as a variable and expands it. Since, it's probably not set it just prints .b.

    Related: When do we need curly braces in variables using Bash?