Replace slash in to empty in bash
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
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?
bash macos resx slash
|
show 1 more comment
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?
bash macos resx slash
1
Your Bash syntax is all wrong. Please post the actual commands you're running.
– ruakh
Nov 15 '18 at 16:09
You are getting what appears to be an error message in trying to runtest/as a command, not the contents ofPRO_VAL(which remains empty, so the "output" ofecho $PRO_VALappears after the error message.sednever actually produces any output, as it had no input.)
– chepner
Nov 15 '18 at 16:11
if [ "$IS_BUILD_COMMAND" = true ] && [ "$CMD_PRO_NAME_VAL" != "" ] && [ "$MICRO_GW_PROJECT_DIR" != "" ]; then echo $CMD_PRO_NAME_VAL echo "$CMD_PRO_NAME_VAL"|sed 's#/##g'
– Hiranya Kavishani
Nov 15 '18 at 16:12
here $CMD_PRO_NAME_VAL = test/ and echo "$CMD_PRO_NAME_VAL"|sed 's#/##g' I got it as "test". I want to take it to variable and print
– Hiranya Kavishani
Nov 15 '18 at 16:13
@chepner yes.how should I fix it?
– Hiranya Kavishani
Nov 15 '18 at 16:21
|
show 1 more comment
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?
bash macos resx slash
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?
bash macos resx slash
bash macos resx slash
edited Nov 15 '18 at 16:09
chepner
263k36253345
263k36253345
asked Nov 15 '18 at 16:05
Hiranya KavishaniHiranya Kavishani
72
72
1
Your Bash syntax is all wrong. Please post the actual commands you're running.
– ruakh
Nov 15 '18 at 16:09
You are getting what appears to be an error message in trying to runtest/as a command, not the contents ofPRO_VAL(which remains empty, so the "output" ofecho $PRO_VALappears after the error message.sednever actually produces any output, as it had no input.)
– chepner
Nov 15 '18 at 16:11
if [ "$IS_BUILD_COMMAND" = true ] && [ "$CMD_PRO_NAME_VAL" != "" ] && [ "$MICRO_GW_PROJECT_DIR" != "" ]; then echo $CMD_PRO_NAME_VAL echo "$CMD_PRO_NAME_VAL"|sed 's#/##g'
– Hiranya Kavishani
Nov 15 '18 at 16:12
here $CMD_PRO_NAME_VAL = test/ and echo "$CMD_PRO_NAME_VAL"|sed 's#/##g' I got it as "test". I want to take it to variable and print
– Hiranya Kavishani
Nov 15 '18 at 16:13
@chepner yes.how should I fix it?
– Hiranya Kavishani
Nov 15 '18 at 16:21
|
show 1 more comment
1
Your Bash syntax is all wrong. Please post the actual commands you're running.
– ruakh
Nov 15 '18 at 16:09
You are getting what appears to be an error message in trying to runtest/as a command, not the contents ofPRO_VAL(which remains empty, so the "output" ofecho $PRO_VALappears after the error message.sednever actually produces any output, as it had no input.)
– chepner
Nov 15 '18 at 16:11
if [ "$IS_BUILD_COMMAND" = true ] && [ "$CMD_PRO_NAME_VAL" != "" ] && [ "$MICRO_GW_PROJECT_DIR" != "" ]; then echo $CMD_PRO_NAME_VAL echo "$CMD_PRO_NAME_VAL"|sed 's#/##g'
– Hiranya Kavishani
Nov 15 '18 at 16:12
here $CMD_PRO_NAME_VAL = test/ and echo "$CMD_PRO_NAME_VAL"|sed 's#/##g' I got it as "test". I want to take it to variable and print
– Hiranya Kavishani
Nov 15 '18 at 16:13
@chepner yes.how should I fix it?
– Hiranya Kavishani
Nov 15 '18 at 16:21
1
1
Your Bash syntax is all wrong. Please post the actual commands you're running.
– ruakh
Nov 15 '18 at 16:09
Your Bash syntax is all wrong. Please post the actual commands you're running.
– ruakh
Nov 15 '18 at 16:09
You are getting what appears to be an error message in trying to run
test/ as a command, not the contents of PRO_VAL (which remains empty, so the "output" of echo $PRO_VAL appears after the error message. sed never actually produces any output, as it had no input.)– chepner
Nov 15 '18 at 16:11
You are getting what appears to be an error message in trying to run
test/ as a command, not the contents of PRO_VAL (which remains empty, so the "output" of echo $PRO_VAL appears after the error message. sed never actually produces any output, as it had no input.)– chepner
Nov 15 '18 at 16:11
if [ "$IS_BUILD_COMMAND" = true ] && [ "$CMD_PRO_NAME_VAL" != "" ] && [ "$MICRO_GW_PROJECT_DIR" != "" ]; then echo $CMD_PRO_NAME_VAL echo "$CMD_PRO_NAME_VAL"|sed 's#/##g'
– Hiranya Kavishani
Nov 15 '18 at 16:12
if [ "$IS_BUILD_COMMAND" = true ] && [ "$CMD_PRO_NAME_VAL" != "" ] && [ "$MICRO_GW_PROJECT_DIR" != "" ]; then echo $CMD_PRO_NAME_VAL echo "$CMD_PRO_NAME_VAL"|sed 's#/##g'
– Hiranya Kavishani
Nov 15 '18 at 16:12
here $CMD_PRO_NAME_VAL = test/ and echo "$CMD_PRO_NAME_VAL"|sed 's#/##g' I got it as "test". I want to take it to variable and print
– Hiranya Kavishani
Nov 15 '18 at 16:13
here $CMD_PRO_NAME_VAL = test/ and echo "$CMD_PRO_NAME_VAL"|sed 's#/##g' I got it as "test". I want to take it to variable and print
– Hiranya Kavishani
Nov 15 '18 at 16:13
@chepner yes.how should I fix it?
– Hiranya Kavishani
Nov 15 '18 at 16:21
@chepner yes.how should I fix it?
– Hiranya Kavishani
Nov 15 '18 at 16:21
|
show 1 more comment
2 Answers
2
active
oldest
votes
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.
Please always double-quote variable references (e.g.echo "$CMD_VAL"), to prevent weird parsing under some circumstances.
– Gordon Davisson
Nov 15 '18 at 18:51
add a comment |
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
1
Removing all slashes is more complicated, but can be done:"$CMD_VAL////". To explain it: the first//means "replace all occurrences of", then/is an escaped forward slash (escaped since/would otherwise be a delimiter), then/is the delimiter between the pattern and its replacement, and then there's a blank replacement string. Whee!
– Gordon Davisson
Nov 15 '18 at 18:55
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53323420%2freplace-slash-in-to-empty-in-bash%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Please always double-quote variable references (e.g.echo "$CMD_VAL"), to prevent weird parsing under some circumstances.
– Gordon Davisson
Nov 15 '18 at 18:51
add a comment |
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.
Please always double-quote variable references (e.g.echo "$CMD_VAL"), to prevent weird parsing under some circumstances.
– Gordon Davisson
Nov 15 '18 at 18:51
add a comment |
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.
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.
answered Nov 15 '18 at 16:14
3h1Nh7YX243h1Nh7YX24
312
312
Please always double-quote variable references (e.g.echo "$CMD_VAL"), to prevent weird parsing under some circumstances.
– Gordon Davisson
Nov 15 '18 at 18:51
add a comment |
Please always double-quote variable references (e.g.echo "$CMD_VAL"), to prevent weird parsing under some circumstances.
– Gordon Davisson
Nov 15 '18 at 18:51
Please always double-quote variable references (e.g.
echo "$CMD_VAL"), to prevent weird parsing under some circumstances.– Gordon Davisson
Nov 15 '18 at 18:51
Please always double-quote variable references (e.g.
echo "$CMD_VAL"), to prevent weird parsing under some circumstances.– Gordon Davisson
Nov 15 '18 at 18:51
add a comment |
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
1
Removing all slashes is more complicated, but can be done:"$CMD_VAL////". To explain it: the first//means "replace all occurrences of", then/is an escaped forward slash (escaped since/would otherwise be a delimiter), then/is the delimiter between the pattern and its replacement, and then there's a blank replacement string. Whee!
– Gordon Davisson
Nov 15 '18 at 18:55
add a comment |
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
1
Removing all slashes is more complicated, but can be done:"$CMD_VAL////". To explain it: the first//means "replace all occurrences of", then/is an escaped forward slash (escaped since/would otherwise be a delimiter), then/is the delimiter between the pattern and its replacement, and then there's a blank replacement string. Whee!
– Gordon Davisson
Nov 15 '18 at 18:55
add a comment |
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
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
answered Nov 15 '18 at 16:15
Paul HodgesPaul Hodges
4,0711524
4,0711524
1
Removing all slashes is more complicated, but can be done:"$CMD_VAL////". To explain it: the first//means "replace all occurrences of", then/is an escaped forward slash (escaped since/would otherwise be a delimiter), then/is the delimiter between the pattern and its replacement, and then there's a blank replacement string. Whee!
– Gordon Davisson
Nov 15 '18 at 18:55
add a comment |
1
Removing all slashes is more complicated, but can be done:"$CMD_VAL////". To explain it: the first//means "replace all occurrences of", then/is an escaped forward slash (escaped since/would otherwise be a delimiter), then/is the delimiter between the pattern and its replacement, and then there's a blank replacement string. Whee!
– Gordon Davisson
Nov 15 '18 at 18:55
1
1
Removing all slashes is more complicated, but can be done:
"$CMD_VAL////". To explain it: the first // means "replace all occurrences of", then / is an escaped forward slash (escaped since / would otherwise be a delimiter), then / is the delimiter between the pattern and its replacement, and then there's a blank replacement string. Whee!– Gordon Davisson
Nov 15 '18 at 18:55
Removing all slashes is more complicated, but can be done:
"$CMD_VAL////". To explain it: the first // means "replace all occurrences of", then / is an escaped forward slash (escaped since / would otherwise be a delimiter), then / is the delimiter between the pattern and its replacement, and then there's a blank replacement string. Whee!– Gordon Davisson
Nov 15 '18 at 18:55
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53323420%2freplace-slash-in-to-empty-in-bash%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Your Bash syntax is all wrong. Please post the actual commands you're running.
– ruakh
Nov 15 '18 at 16:09
You are getting what appears to be an error message in trying to run
test/as a command, not the contents ofPRO_VAL(which remains empty, so the "output" ofecho $PRO_VALappears after the error message.sednever actually produces any output, as it had no input.)– chepner
Nov 15 '18 at 16:11
if [ "$IS_BUILD_COMMAND" = true ] && [ "$CMD_PRO_NAME_VAL" != "" ] && [ "$MICRO_GW_PROJECT_DIR" != "" ]; then echo $CMD_PRO_NAME_VAL echo "$CMD_PRO_NAME_VAL"|sed 's#/##g'
– Hiranya Kavishani
Nov 15 '18 at 16:12
here $CMD_PRO_NAME_VAL = test/ and echo "$CMD_PRO_NAME_VAL"|sed 's#/##g' I got it as "test". I want to take it to variable and print
– Hiranya Kavishani
Nov 15 '18 at 16:13
@chepner yes.how should I fix it?
– Hiranya Kavishani
Nov 15 '18 at 16:21