Discussion:
[Help-bash] Is there a way to combine $( and ${%}?
Peng Yu
2017-12-08 03:58:34 UTC
Permalink
Hi,

I have the following commands. Is there a way to combine them into one command?

x=$(readlink -e "$0")
x=${x%/*}
--
Regards,
Peng
Greg Wooledge
2017-12-08 13:10:59 UTC
Permalink
Post by Peng Yu
Hi,
I have the following commands. Is there a way to combine them into one command?
x=$(readlink -e "$0")
x=${x%/*}
x=$(readlink -e "$0") x=${x%/*}

There you go. Now it's one command.
John McKown
2017-12-08 13:22:33 UTC
Permalink
Post by Peng Yu
Post by Peng Yu
Hi,
I have the following commands. Is there a way to combine them into one
command?
Post by Peng Yu
x=$(readlink -e "$0")
x=${x%/*}
x=$(readlink -e "$0") x=${x%/*}
There you go. Now it's one command.
​Works. But if the OP wants another technically correct, but worse, answer,
there is always:

x=$(readlink -e "$0" | sed -r 's|^(.*)/[^/]*$|\1|' )​
--
I have a theory that it's impossible to prove anything, but I can't prove
it.

Maranatha! <><
John McKown
Greg Wooledge
2017-12-08 13:47:36 UTC
Permalink
Post by John McKown
Works. But if the OP wants another technically correct, but worse, answer,
x=$(readlink -e "$0" | sed -r 's|^(.*)/[^/]*$|\1|' )
Gotta love seeing one nonportable command with a raising with a second
nonportable command.

Also, as long as I'm in this thread:

https://mywiki.wooledge.org/BashFAQ/028

Loading...