The simplest way to “subtrack” a string variable in terminal?
Let's assume that we've a variable $test
which holds a value asd 123
- what is the most simplest way to cut, for example, asd
part?
I've googled this for awhile and surprised that I couldn't find an answer.
command-line
add a comment |
Let's assume that we've a variable $test
which holds a value asd 123
- what is the most simplest way to cut, for example, asd
part?
I've googled this for awhile and surprised that I couldn't find an answer.
command-line
add a comment |
Let's assume that we've a variable $test
which holds a value asd 123
- what is the most simplest way to cut, for example, asd
part?
I've googled this for awhile and surprised that I couldn't find an answer.
command-line
Let's assume that we've a variable $test
which holds a value asd 123
- what is the most simplest way to cut, for example, asd
part?
I've googled this for awhile and surprised that I couldn't find an answer.
command-line
command-line
asked Jan 4 at 6:03
Cecily MillerCecily Miller
10115
10115
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
In Bourne-like shells, such as dash
( /bin/sh
on Ubuntu ), bash
and ksh
there is something known as parameter expansion, and is a feature specified by POSIX standard. Specifically, to quote dash
manual:
${parameter%word} Remove Smallest Suffix Pattern. The word is expanded to produce a pattern. The parameter expansion then results in parameter, with the smallest
portion of the suffix matched by the pattern deleted.
${parameter%%word} Remove Largest Suffix Pattern. The word is expanded to produce a pattern. The parameter expansion then results in parameter, with the largest
portion of the suffix matched by the pattern deleted.
${parameter#word} Remove Smallest Prefix Pattern. The word is expanded to produce a pattern. The parameter expansion then results in parameter, with the smallest
portion of the prefix matched by the pattern deleted.
${parameter##word} Remove Largest Prefix Pattern. The word is expanded to produce a pattern. The parameter expansion then results in parameter, with the largest
portion of the prefix matched by the pattern deleted.
Thus, to replace asd
part you can do:
$ var="asd 123"
$ echo ${var#asd*}
123
To remove 123
you can do:
$ echo ${var%*123}
asd
bash
goes even further with this with another form: ${parameter/string/replacement}
and ${parameter//string/replacement}
. First one replaces first occurrence of string. Second form replaces all occurrences. For instance:
$ echo ${var//123/}
asd
$ echo ${var//asd/}
123
Note that according with the syntax, the replacement
part is empty string
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
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%2faskubuntu.com%2fquestions%2f1106812%2fthe-simplest-way-to-subtrack-a-string-variable-in-terminal%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
In Bourne-like shells, such as dash
( /bin/sh
on Ubuntu ), bash
and ksh
there is something known as parameter expansion, and is a feature specified by POSIX standard. Specifically, to quote dash
manual:
${parameter%word} Remove Smallest Suffix Pattern. The word is expanded to produce a pattern. The parameter expansion then results in parameter, with the smallest
portion of the suffix matched by the pattern deleted.
${parameter%%word} Remove Largest Suffix Pattern. The word is expanded to produce a pattern. The parameter expansion then results in parameter, with the largest
portion of the suffix matched by the pattern deleted.
${parameter#word} Remove Smallest Prefix Pattern. The word is expanded to produce a pattern. The parameter expansion then results in parameter, with the smallest
portion of the prefix matched by the pattern deleted.
${parameter##word} Remove Largest Prefix Pattern. The word is expanded to produce a pattern. The parameter expansion then results in parameter, with the largest
portion of the prefix matched by the pattern deleted.
Thus, to replace asd
part you can do:
$ var="asd 123"
$ echo ${var#asd*}
123
To remove 123
you can do:
$ echo ${var%*123}
asd
bash
goes even further with this with another form: ${parameter/string/replacement}
and ${parameter//string/replacement}
. First one replaces first occurrence of string. Second form replaces all occurrences. For instance:
$ echo ${var//123/}
asd
$ echo ${var//asd/}
123
Note that according with the syntax, the replacement
part is empty string
add a comment |
In Bourne-like shells, such as dash
( /bin/sh
on Ubuntu ), bash
and ksh
there is something known as parameter expansion, and is a feature specified by POSIX standard. Specifically, to quote dash
manual:
${parameter%word} Remove Smallest Suffix Pattern. The word is expanded to produce a pattern. The parameter expansion then results in parameter, with the smallest
portion of the suffix matched by the pattern deleted.
${parameter%%word} Remove Largest Suffix Pattern. The word is expanded to produce a pattern. The parameter expansion then results in parameter, with the largest
portion of the suffix matched by the pattern deleted.
${parameter#word} Remove Smallest Prefix Pattern. The word is expanded to produce a pattern. The parameter expansion then results in parameter, with the smallest
portion of the prefix matched by the pattern deleted.
${parameter##word} Remove Largest Prefix Pattern. The word is expanded to produce a pattern. The parameter expansion then results in parameter, with the largest
portion of the prefix matched by the pattern deleted.
Thus, to replace asd
part you can do:
$ var="asd 123"
$ echo ${var#asd*}
123
To remove 123
you can do:
$ echo ${var%*123}
asd
bash
goes even further with this with another form: ${parameter/string/replacement}
and ${parameter//string/replacement}
. First one replaces first occurrence of string. Second form replaces all occurrences. For instance:
$ echo ${var//123/}
asd
$ echo ${var//asd/}
123
Note that according with the syntax, the replacement
part is empty string
add a comment |
In Bourne-like shells, such as dash
( /bin/sh
on Ubuntu ), bash
and ksh
there is something known as parameter expansion, and is a feature specified by POSIX standard. Specifically, to quote dash
manual:
${parameter%word} Remove Smallest Suffix Pattern. The word is expanded to produce a pattern. The parameter expansion then results in parameter, with the smallest
portion of the suffix matched by the pattern deleted.
${parameter%%word} Remove Largest Suffix Pattern. The word is expanded to produce a pattern. The parameter expansion then results in parameter, with the largest
portion of the suffix matched by the pattern deleted.
${parameter#word} Remove Smallest Prefix Pattern. The word is expanded to produce a pattern. The parameter expansion then results in parameter, with the smallest
portion of the prefix matched by the pattern deleted.
${parameter##word} Remove Largest Prefix Pattern. The word is expanded to produce a pattern. The parameter expansion then results in parameter, with the largest
portion of the prefix matched by the pattern deleted.
Thus, to replace asd
part you can do:
$ var="asd 123"
$ echo ${var#asd*}
123
To remove 123
you can do:
$ echo ${var%*123}
asd
bash
goes even further with this with another form: ${parameter/string/replacement}
and ${parameter//string/replacement}
. First one replaces first occurrence of string. Second form replaces all occurrences. For instance:
$ echo ${var//123/}
asd
$ echo ${var//asd/}
123
Note that according with the syntax, the replacement
part is empty string
In Bourne-like shells, such as dash
( /bin/sh
on Ubuntu ), bash
and ksh
there is something known as parameter expansion, and is a feature specified by POSIX standard. Specifically, to quote dash
manual:
${parameter%word} Remove Smallest Suffix Pattern. The word is expanded to produce a pattern. The parameter expansion then results in parameter, with the smallest
portion of the suffix matched by the pattern deleted.
${parameter%%word} Remove Largest Suffix Pattern. The word is expanded to produce a pattern. The parameter expansion then results in parameter, with the largest
portion of the suffix matched by the pattern deleted.
${parameter#word} Remove Smallest Prefix Pattern. The word is expanded to produce a pattern. The parameter expansion then results in parameter, with the smallest
portion of the prefix matched by the pattern deleted.
${parameter##word} Remove Largest Prefix Pattern. The word is expanded to produce a pattern. The parameter expansion then results in parameter, with the largest
portion of the prefix matched by the pattern deleted.
Thus, to replace asd
part you can do:
$ var="asd 123"
$ echo ${var#asd*}
123
To remove 123
you can do:
$ echo ${var%*123}
asd
bash
goes even further with this with another form: ${parameter/string/replacement}
and ${parameter//string/replacement}
. First one replaces first occurrence of string. Second form replaces all occurrences. For instance:
$ echo ${var//123/}
asd
$ echo ${var//asd/}
123
Note that according with the syntax, the replacement
part is empty string
answered Jan 4 at 6:46
Sergiy KolodyazhnyySergiy Kolodyazhnyy
70.6k9147310
70.6k9147310
add a comment |
add a comment |
Thanks for contributing an answer to Ask Ubuntu!
- 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%2faskubuntu.com%2fquestions%2f1106812%2fthe-simplest-way-to-subtrack-a-string-variable-in-terminal%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