1

I'm trying to write a script that accepts a parameter ($1) with backslashes and I want my script to echo the parameter ($1) exactly with the backslashes entered back.

e.g.

$ ./tst \\abc\def\ghi\jkl\lmn\
\\abc\def\ghi\jkl\lmn\

My tst script at present looks like this;

#!/bin/bash
echo $1

When I run my script it returns;

\abcdefghijkllmn

I want it to return:

\\abc\def\ghi\jkl\lmn\

Exactly what I entered. I've even tried echo -E $1 but that made no difference.

Any suggestions I could achieve my desirable returned output from my script would be very much appreciated.

Eric Renouf
  • 18,431

1 Answers1

5

Try passing the parameter(s) to your script using single quotes ('). Without that, your shell is eating them before your script even sees the parameter.

$ ./tst '\\abc\def\ghi\jkl\lmn\'
Soruk
  • 251
  • 1
    You script should also use echo "$1"; unquoted, the shell will process $1 before passing the result to echo. – chepner Sep 21 '17 at 12:01