Temporarily declare a variable in Bash
up vote
1
down vote
favorite
To declare a variable in Bash (say in a Bash script-file and not in Bash function) I do for example:
x=y
But when I finish using it inside that script-file I do unset x
.
Is there a way (without using a function), to unset the variable after say 5 minutes, both in one line?
A plausible approach might be x=y && echo "unset x" | at now + 5 minutes
.
bash variable at
add a comment |
up vote
1
down vote
favorite
To declare a variable in Bash (say in a Bash script-file and not in Bash function) I do for example:
x=y
But when I finish using it inside that script-file I do unset x
.
Is there a way (without using a function), to unset the variable after say 5 minutes, both in one line?
A plausible approach might be x=y && echo "unset x" | at now + 5 minutes
.
bash variable at
1
You need to dounset x
only if you are sourcing the script file. Otherwise the script runs in a subshell and does not affect your shell environment, so you don't need to unset it. I use parentheses to run one-liners with temporary variables on the command line all the time, like( for i in {1..20}; do dosomething; done)
, and after I execute this command I don't have$i
in my shell.
– Weijun Zhou
47 mins ago
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
To declare a variable in Bash (say in a Bash script-file and not in Bash function) I do for example:
x=y
But when I finish using it inside that script-file I do unset x
.
Is there a way (without using a function), to unset the variable after say 5 minutes, both in one line?
A plausible approach might be x=y && echo "unset x" | at now + 5 minutes
.
bash variable at
To declare a variable in Bash (say in a Bash script-file and not in Bash function) I do for example:
x=y
But when I finish using it inside that script-file I do unset x
.
Is there a way (without using a function), to unset the variable after say 5 minutes, both in one line?
A plausible approach might be x=y && echo "unset x" | at now + 5 minutes
.
bash variable at
bash variable at
asked 2 hours ago
JohnDoea
691132
691132
1
You need to dounset x
only if you are sourcing the script file. Otherwise the script runs in a subshell and does not affect your shell environment, so you don't need to unset it. I use parentheses to run one-liners with temporary variables on the command line all the time, like( for i in {1..20}; do dosomething; done)
, and after I execute this command I don't have$i
in my shell.
– Weijun Zhou
47 mins ago
add a comment |
1
You need to dounset x
only if you are sourcing the script file. Otherwise the script runs in a subshell and does not affect your shell environment, so you don't need to unset it. I use parentheses to run one-liners with temporary variables on the command line all the time, like( for i in {1..20}; do dosomething; done)
, and after I execute this command I don't have$i
in my shell.
– Weijun Zhou
47 mins ago
1
1
You need to do
unset x
only if you are sourcing the script file. Otherwise the script runs in a subshell and does not affect your shell environment, so you don't need to unset it. I use parentheses to run one-liners with temporary variables on the command line all the time, like ( for i in {1..20}; do dosomething; done)
, and after I execute this command I don't have $i
in my shell.– Weijun Zhou
47 mins ago
You need to do
unset x
only if you are sourcing the script file. Otherwise the script runs in a subshell and does not affect your shell environment, so you don't need to unset it. I use parentheses to run one-liners with temporary variables on the command line all the time, like ( for i in {1..20}; do dosomething; done)
, and after I execute this command I don't have $i
in my shell.– Weijun Zhou
47 mins ago
add a comment |
2 Answers
2
active
oldest
votes
up vote
4
down vote
You can't use at
jobs because they run in a different context, and can't affect the current shell.
But we can do something similar. This code will trigger an alarm signal, which we can catch and perform action on
#!/bin/bash
x=100
trap 'unset x' SIGALRM
mypid=$$
( /bin/sleep 3 ; kill -ALRM $mypid) &
for a in 1 2 3 4 5 6
do
echo Now x=$x
sleep 1
done
This example is only 3 seconds long to demonstrate the solution; you can pick your delay as you need.
In action:
Now x=100
Now x=100
Now x=100
Now x=
Now x=
Now x=
You can easily make it one line with ;
...
Wow - great minds think alike -- and within 30 seconds of each other! :)
– Jeff Schaller
1 hour ago
@JeffSchaller I noticed that :-)
– Stephen Harris
1 hour ago
1
@JeffSchaller I rolled back your edit; I specifically put a 3 second pause in as an example; a 300 second (5minute) sleep would mean thefor
loop won't demonstrate the solution working. I'll edit the answer to make that clear.
– Stephen Harris
1 hour ago
add a comment |
up vote
3
down vote
Sure:
trap 'unset x; trap - USR1' USR1; { sleep 5m; kill -USR1 $$; } &
This sets a trap on the USR1 signal, then (cheating with a semicolon to put it on one line) groups together the sleep and kill commands into a background job. When that job completes, it will send the USR1 signal to the current shell, which will execute the trap. The trap unsets x
and then clears the trap.
No functions!
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
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',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2funix.stackexchange.com%2fquestions%2f487683%2ftemporarily-declare-a-variable-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
up vote
4
down vote
You can't use at
jobs because they run in a different context, and can't affect the current shell.
But we can do something similar. This code will trigger an alarm signal, which we can catch and perform action on
#!/bin/bash
x=100
trap 'unset x' SIGALRM
mypid=$$
( /bin/sleep 3 ; kill -ALRM $mypid) &
for a in 1 2 3 4 5 6
do
echo Now x=$x
sleep 1
done
This example is only 3 seconds long to demonstrate the solution; you can pick your delay as you need.
In action:
Now x=100
Now x=100
Now x=100
Now x=
Now x=
Now x=
You can easily make it one line with ;
...
Wow - great minds think alike -- and within 30 seconds of each other! :)
– Jeff Schaller
1 hour ago
@JeffSchaller I noticed that :-)
– Stephen Harris
1 hour ago
1
@JeffSchaller I rolled back your edit; I specifically put a 3 second pause in as an example; a 300 second (5minute) sleep would mean thefor
loop won't demonstrate the solution working. I'll edit the answer to make that clear.
– Stephen Harris
1 hour ago
add a comment |
up vote
4
down vote
You can't use at
jobs because they run in a different context, and can't affect the current shell.
But we can do something similar. This code will trigger an alarm signal, which we can catch and perform action on
#!/bin/bash
x=100
trap 'unset x' SIGALRM
mypid=$$
( /bin/sleep 3 ; kill -ALRM $mypid) &
for a in 1 2 3 4 5 6
do
echo Now x=$x
sleep 1
done
This example is only 3 seconds long to demonstrate the solution; you can pick your delay as you need.
In action:
Now x=100
Now x=100
Now x=100
Now x=
Now x=
Now x=
You can easily make it one line with ;
...
Wow - great minds think alike -- and within 30 seconds of each other! :)
– Jeff Schaller
1 hour ago
@JeffSchaller I noticed that :-)
– Stephen Harris
1 hour ago
1
@JeffSchaller I rolled back your edit; I specifically put a 3 second pause in as an example; a 300 second (5minute) sleep would mean thefor
loop won't demonstrate the solution working. I'll edit the answer to make that clear.
– Stephen Harris
1 hour ago
add a comment |
up vote
4
down vote
up vote
4
down vote
You can't use at
jobs because they run in a different context, and can't affect the current shell.
But we can do something similar. This code will trigger an alarm signal, which we can catch and perform action on
#!/bin/bash
x=100
trap 'unset x' SIGALRM
mypid=$$
( /bin/sleep 3 ; kill -ALRM $mypid) &
for a in 1 2 3 4 5 6
do
echo Now x=$x
sleep 1
done
This example is only 3 seconds long to demonstrate the solution; you can pick your delay as you need.
In action:
Now x=100
Now x=100
Now x=100
Now x=
Now x=
Now x=
You can easily make it one line with ;
...
You can't use at
jobs because they run in a different context, and can't affect the current shell.
But we can do something similar. This code will trigger an alarm signal, which we can catch and perform action on
#!/bin/bash
x=100
trap 'unset x' SIGALRM
mypid=$$
( /bin/sleep 3 ; kill -ALRM $mypid) &
for a in 1 2 3 4 5 6
do
echo Now x=$x
sleep 1
done
This example is only 3 seconds long to demonstrate the solution; you can pick your delay as you need.
In action:
Now x=100
Now x=100
Now x=100
Now x=
Now x=
Now x=
You can easily make it one line with ;
...
edited 1 hour ago
answered 1 hour ago
Stephen Harris
23.9k24477
23.9k24477
Wow - great minds think alike -- and within 30 seconds of each other! :)
– Jeff Schaller
1 hour ago
@JeffSchaller I noticed that :-)
– Stephen Harris
1 hour ago
1
@JeffSchaller I rolled back your edit; I specifically put a 3 second pause in as an example; a 300 second (5minute) sleep would mean thefor
loop won't demonstrate the solution working. I'll edit the answer to make that clear.
– Stephen Harris
1 hour ago
add a comment |
Wow - great minds think alike -- and within 30 seconds of each other! :)
– Jeff Schaller
1 hour ago
@JeffSchaller I noticed that :-)
– Stephen Harris
1 hour ago
1
@JeffSchaller I rolled back your edit; I specifically put a 3 second pause in as an example; a 300 second (5minute) sleep would mean thefor
loop won't demonstrate the solution working. I'll edit the answer to make that clear.
– Stephen Harris
1 hour ago
Wow - great minds think alike -- and within 30 seconds of each other! :)
– Jeff Schaller
1 hour ago
Wow - great minds think alike -- and within 30 seconds of each other! :)
– Jeff Schaller
1 hour ago
@JeffSchaller I noticed that :-)
– Stephen Harris
1 hour ago
@JeffSchaller I noticed that :-)
– Stephen Harris
1 hour ago
1
1
@JeffSchaller I rolled back your edit; I specifically put a 3 second pause in as an example; a 300 second (5minute) sleep would mean the
for
loop won't demonstrate the solution working. I'll edit the answer to make that clear.– Stephen Harris
1 hour ago
@JeffSchaller I rolled back your edit; I specifically put a 3 second pause in as an example; a 300 second (5minute) sleep would mean the
for
loop won't demonstrate the solution working. I'll edit the answer to make that clear.– Stephen Harris
1 hour ago
add a comment |
up vote
3
down vote
Sure:
trap 'unset x; trap - USR1' USR1; { sleep 5m; kill -USR1 $$; } &
This sets a trap on the USR1 signal, then (cheating with a semicolon to put it on one line) groups together the sleep and kill commands into a background job. When that job completes, it will send the USR1 signal to the current shell, which will execute the trap. The trap unsets x
and then clears the trap.
No functions!
add a comment |
up vote
3
down vote
Sure:
trap 'unset x; trap - USR1' USR1; { sleep 5m; kill -USR1 $$; } &
This sets a trap on the USR1 signal, then (cheating with a semicolon to put it on one line) groups together the sleep and kill commands into a background job. When that job completes, it will send the USR1 signal to the current shell, which will execute the trap. The trap unsets x
and then clears the trap.
No functions!
add a comment |
up vote
3
down vote
up vote
3
down vote
Sure:
trap 'unset x; trap - USR1' USR1; { sleep 5m; kill -USR1 $$; } &
This sets a trap on the USR1 signal, then (cheating with a semicolon to put it on one line) groups together the sleep and kill commands into a background job. When that job completes, it will send the USR1 signal to the current shell, which will execute the trap. The trap unsets x
and then clears the trap.
No functions!
Sure:
trap 'unset x; trap - USR1' USR1; { sleep 5m; kill -USR1 $$; } &
This sets a trap on the USR1 signal, then (cheating with a semicolon to put it on one line) groups together the sleep and kill commands into a background job. When that job completes, it will send the USR1 signal to the current shell, which will execute the trap. The trap unsets x
and then clears the trap.
No functions!
edited 1 hour ago
answered 1 hour ago
Jeff Schaller
37.6k1052121
37.6k1052121
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- 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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2funix.stackexchange.com%2fquestions%2f487683%2ftemporarily-declare-a-variable-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
You need to do
unset x
only if you are sourcing the script file. Otherwise the script runs in a subshell and does not affect your shell environment, so you don't need to unset it. I use parentheses to run one-liners with temporary variables on the command line all the time, like( for i in {1..20}; do dosomething; done)
, and after I execute this command I don't have$i
in my shell.– Weijun Zhou
47 mins ago