0

So I'm trying to compose a string using script shell

#!/bin/sh blNumber=1111619832 echo '*'$blNumber+='*.xlsx' 

I expect the output to be: *1111619832*.xlsx but as a result I get: *+=*.xlsx

Btw I tried to figure out why so I tried this code

#!/bin/sh blNumber=1111619832 output="*${blNumber}" 

and whenever I add something after *${blNumber} it get concatenated at the begging of the string

4
  • 1
    Watch your shebang! sh isn't necessarily bash. Commented Jul 1, 2021 at 16:06
  • @HamaLAHLOU : Removed bash tag, because the question is not bash-related. Commented Jul 2, 2021 at 7:34
  • @HamaLAHLOU : Just use b1Numer="$b1Number.xlsx", or more general, b1Numer="${b1Number}.xlsx" Commented Jul 2, 2021 at 7:35
  • @HamzaLAHOU : You can - with few exceptions, which don't apply here - not modify a variable inside another statement. You have to first write a statement which changes the variable, and then you can use it in i.e. an echo. Commented Jul 2, 2021 at 7:37

1 Answer 1

1

Why are you using += in the first place?

$ echo '*'$blNumber'*.xlsx' *1111619832*.xlsx 

Or put it inside double-quotes. It's best practice to quote all variables anyway.

$ echo "*$blNumber*.xlsx" *1111619832*.xlsx 
Sign up to request clarification or add additional context in comments.

4 Comments

I tried your solution but ut's not working. the output : *.xlsx19832
@Hamza It looks like you have Windows line endings. See How to convert DOS/Windows newline (CRLF) to Unix newline (LF) in a Bash script
the link above solved the issue on notepad++

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.