Hiranya Kavishani
Hiranya Kavishani

Reputation: 17

Replace slash in to empty in bash

Let's suppose I have this results:

CMD_VAL = 'test/'
echo $CMD_VAL
=> test/

echo "$CMD_VAL"|sed 's#/##g'
=>test

but,

PRO_VAL = "$CMD_VAL"|sed 's#/##g'
echo $PRO_VAL

this returns

=> "test/ is a directory"

How should it need to change in order to get the "test" into a variable as a string?

Upvotes: 0

Views: 3416

Answers (2)

Paul Hodges
Paul Hodges

Reputation: 15273

No need to spawn an external process. c.f. this cheat-sheet for a guide on things like using the interpreter's built-in string processing tools.

$: CMD_VAL='test/'       # no spaces...
$: CMD_VAL=${CMD_VAL%/}  # strip the training slash
$: echo "$CMD_VAL"
test

Upvotes: 4

3h1Nh7YX24
3h1Nh7YX24

Reputation: 56

PRO_VAL=$(echo $CMD_VAL|sed 's#/##g')

you need to echo first, "$CMD_VAL"|sed 's#/##g" will be run $CMD_VAL and pipe to sed,it's not correct.

Upvotes: 0

Related Questions