简体   繁体   中英

Bash Script: Replace backslash characters in argument?

I have a script called test and I am trying to take in an argument and replace backslashes with forward slashes. If I do the following:

#!/bin/bash

FIRST_ARGUMENT=$1

echo $FIRST_ARGUMENT | sed 's/\\/\//g'

And I use it in the following way:

$ ./test  aaa\aa    
aaaaa

My backslashes disappear. Is there a way to preserve them so they are replaced by sed? When I hardcode the path into a string within my echo call, as in:

echo 'aaa\aa'| sed 's/\\/\//g'

I get the proper result:

aaa/aa

While passing it to the ./test , use quote ' or " as:

./test 'aaa\aa'

Below is the example to show difference between content with quote and without quote:

moin@moin-pc:~$ echo aa\aa
aaaa  # without '\'
moin@moin-pc:~$ echo "aa\aa"
aa\aa  # with '\'

Also read: Meaning of backslash

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM