Invoking pushd in a shell scriptDifferent ways to execute a shell scriptchanging current working dir with a scriptTiming out in a shell scriptshell commands to check & create dirRule for invoking subshell in Bash?Interactive shell in background inside another shell scriptHow to run privileged bash script as non-root?Source shell script automatically in terminalHow can I search and execute a shell script from another?self killing shell scriptRun shell script at startup (Kali Linux)Importing environment variables from another script into the current shell
Inorganic chemistry handbook with reaction lists
Generating a list with duplicate entries
Is it a Cyclops number? "Nobody" knows!
Should I apply for my boss's promotion?
Unfamiliar notation in Diabelli's "Duet in D" for piano
I am the person who abides by rules but breaks the rules . Who am I
Should I file my taxes? No income, unemployed, but paid 2k in student loan interest
What exactly is the meaning of "fine wine"?
Averaging over columns while ignoring zero entries
Are small insurances worth it?
Is there a logarithm base for which the logarithm becomes an identity function?
Too soon for a plot twist?
What does "rhumatis" mean?
Is it appropriate to ask a former professor to order a library book for me through ILL?
What is Tony Stark injecting into himself in Iron Man 3?
Who has more? Ireland or Iceland?
How would an energy-based "projectile" blow up a spaceship?
Use Mercury as quenching liquid for swords?
Why does this boat have a landing pad? (SpaceX's GO Searcher) Any plans for propulsive capsule landings?
Rationale to prefer local variables over instance variables?
ESPP--any reason not to go all in?
What do you call someone who likes to pick fights?
What does *dead* mean in *What do you mean, dead?*?
Paper published similar to PhD thesis
Invoking pushd in a shell script
Different ways to execute a shell scriptchanging current working dir with a scriptTiming out in a shell scriptshell commands to check & create dirRule for invoking subshell in Bash?Interactive shell in background inside another shell scriptHow to run privileged bash script as non-root?Source shell script automatically in terminalHow can I search and execute a shell script from another?self killing shell scriptRun shell script at startup (Kali Linux)Importing environment variables from another script into the current shell
I have a simple script that I want to invoke 'pushed' followed by another command. But the 'pushd' command inside the script doesn't seem to stick past the script.
What's a way to make this execute in the shell terminal?
#!/bin/sh
pushd $1
time
What I really want to accomplish is invoke pushd
followed by other-command
with one command.
command-line shell
add a comment |
I have a simple script that I want to invoke 'pushed' followed by another command. But the 'pushd' command inside the script doesn't seem to stick past the script.
What's a way to make this execute in the shell terminal?
#!/bin/sh
pushd $1
time
What I really want to accomplish is invoke pushd
followed by other-command
with one command.
command-line shell
Related question: unix.stackexchange.com/q/2976/250
– phunehehe
Dec 29 '10 at 0:49
add a comment |
I have a simple script that I want to invoke 'pushed' followed by another command. But the 'pushd' command inside the script doesn't seem to stick past the script.
What's a way to make this execute in the shell terminal?
#!/bin/sh
pushd $1
time
What I really want to accomplish is invoke pushd
followed by other-command
with one command.
command-line shell
I have a simple script that I want to invoke 'pushed' followed by another command. But the 'pushd' command inside the script doesn't seem to stick past the script.
What's a way to make this execute in the shell terminal?
#!/bin/sh
pushd $1
time
What I really want to accomplish is invoke pushd
followed by other-command
with one command.
command-line shell
command-line shell
edited Dec 29 '10 at 0:40
asked Dec 28 '10 at 22:28
Sarah Soto
Related question: unix.stackexchange.com/q/2976/250
– phunehehe
Dec 29 '10 at 0:49
add a comment |
Related question: unix.stackexchange.com/q/2976/250
– phunehehe
Dec 29 '10 at 0:49
Related question: unix.stackexchange.com/q/2976/250
– phunehehe
Dec 29 '10 at 0:49
Related question: unix.stackexchange.com/q/2976/250
– phunehehe
Dec 29 '10 at 0:49
add a comment |
3 Answers
3
active
oldest
votes
A shell script normally executes in a separate instance of the shell program, /bin/sh
in this case. Your pushd
command affects that sub-shell's working directory only. If it were otherwise, any program you ran from the shell could mess with your shell's working directory.
To execute that script within the current shell, say this instead:
$ . my-command somedir
or, more verbosely:
$ source my-command somedir
To make it appear that your program works like any other, you can use an alias:
$ alias mycmd='source my-command'
$ mycmd /bin
$ pwd
/bin
What I really want is execute 'pushd <params>' followed by another command that takes no params. I may not need a shell script for it - what's a good way to do this?
– Sarah Soto
Dec 29 '10 at 0:23
@sarah-soto All you need then ispushd <params>; other-command
– Shawn J. Goff
Dec 29 '10 at 0:26
I should have clarified: I want a shortcut so I am invoking both commands one after the after without implicitly calling both. I can't aliase the two commands to be alias p2='pushd; other-command' and then invoke 'p2 ~/temp' as the shell will return the error: '-bash: pushd: no other directory
– Sarah Soto
Dec 29 '10 at 0:38
You could use a shell function:function p2 () pushd "$@"; other-command;
– Steven D
Dec 29 '10 at 0:46
2
A shell script is a fine way to do this. It's inefficient, but this is not a case where optimization is worth bothering with. Defining a shell function in your~/.bash_profile
(or whatever your shell uses, if not Bash) will also work. Your choice.
– Warren Young
Dec 29 '10 at 3:10
|
show 2 more comments
Scripts cannot alter their parent processe's environment. Because of this, any environment changes made in the script are lost.
To run the script in the same process, you can 'source' the script like this
. /path/to/script.sh args
add a comment |
Instead of a shell script, function is more appropriate for this kind of operation, especially when using pushd.
Add this in .bashrc
:
foo()
pushd $1
time
export foo
In shell:
$ foo mydir1
output:
~/mydir1 ~
real 0m0.000s
user 0m0.000s
sys 0m0.000s
New contributor
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',
autoActivateHeartbeat: false,
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%2f5204%2finvoking-pushd-in-a-shell-script%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
A shell script normally executes in a separate instance of the shell program, /bin/sh
in this case. Your pushd
command affects that sub-shell's working directory only. If it were otherwise, any program you ran from the shell could mess with your shell's working directory.
To execute that script within the current shell, say this instead:
$ . my-command somedir
or, more verbosely:
$ source my-command somedir
To make it appear that your program works like any other, you can use an alias:
$ alias mycmd='source my-command'
$ mycmd /bin
$ pwd
/bin
What I really want is execute 'pushd <params>' followed by another command that takes no params. I may not need a shell script for it - what's a good way to do this?
– Sarah Soto
Dec 29 '10 at 0:23
@sarah-soto All you need then ispushd <params>; other-command
– Shawn J. Goff
Dec 29 '10 at 0:26
I should have clarified: I want a shortcut so I am invoking both commands one after the after without implicitly calling both. I can't aliase the two commands to be alias p2='pushd; other-command' and then invoke 'p2 ~/temp' as the shell will return the error: '-bash: pushd: no other directory
– Sarah Soto
Dec 29 '10 at 0:38
You could use a shell function:function p2 () pushd "$@"; other-command;
– Steven D
Dec 29 '10 at 0:46
2
A shell script is a fine way to do this. It's inefficient, but this is not a case where optimization is worth bothering with. Defining a shell function in your~/.bash_profile
(or whatever your shell uses, if not Bash) will also work. Your choice.
– Warren Young
Dec 29 '10 at 3:10
|
show 2 more comments
A shell script normally executes in a separate instance of the shell program, /bin/sh
in this case. Your pushd
command affects that sub-shell's working directory only. If it were otherwise, any program you ran from the shell could mess with your shell's working directory.
To execute that script within the current shell, say this instead:
$ . my-command somedir
or, more verbosely:
$ source my-command somedir
To make it appear that your program works like any other, you can use an alias:
$ alias mycmd='source my-command'
$ mycmd /bin
$ pwd
/bin
What I really want is execute 'pushd <params>' followed by another command that takes no params. I may not need a shell script for it - what's a good way to do this?
– Sarah Soto
Dec 29 '10 at 0:23
@sarah-soto All you need then ispushd <params>; other-command
– Shawn J. Goff
Dec 29 '10 at 0:26
I should have clarified: I want a shortcut so I am invoking both commands one after the after without implicitly calling both. I can't aliase the two commands to be alias p2='pushd; other-command' and then invoke 'p2 ~/temp' as the shell will return the error: '-bash: pushd: no other directory
– Sarah Soto
Dec 29 '10 at 0:38
You could use a shell function:function p2 () pushd "$@"; other-command;
– Steven D
Dec 29 '10 at 0:46
2
A shell script is a fine way to do this. It's inefficient, but this is not a case where optimization is worth bothering with. Defining a shell function in your~/.bash_profile
(or whatever your shell uses, if not Bash) will also work. Your choice.
– Warren Young
Dec 29 '10 at 3:10
|
show 2 more comments
A shell script normally executes in a separate instance of the shell program, /bin/sh
in this case. Your pushd
command affects that sub-shell's working directory only. If it were otherwise, any program you ran from the shell could mess with your shell's working directory.
To execute that script within the current shell, say this instead:
$ . my-command somedir
or, more verbosely:
$ source my-command somedir
To make it appear that your program works like any other, you can use an alias:
$ alias mycmd='source my-command'
$ mycmd /bin
$ pwd
/bin
A shell script normally executes in a separate instance of the shell program, /bin/sh
in this case. Your pushd
command affects that sub-shell's working directory only. If it were otherwise, any program you ran from the shell could mess with your shell's working directory.
To execute that script within the current shell, say this instead:
$ . my-command somedir
or, more verbosely:
$ source my-command somedir
To make it appear that your program works like any other, you can use an alias:
$ alias mycmd='source my-command'
$ mycmd /bin
$ pwd
/bin
answered Dec 28 '10 at 22:54
Warren YoungWarren Young
55.7k11143148
55.7k11143148
What I really want is execute 'pushd <params>' followed by another command that takes no params. I may not need a shell script for it - what's a good way to do this?
– Sarah Soto
Dec 29 '10 at 0:23
@sarah-soto All you need then ispushd <params>; other-command
– Shawn J. Goff
Dec 29 '10 at 0:26
I should have clarified: I want a shortcut so I am invoking both commands one after the after without implicitly calling both. I can't aliase the two commands to be alias p2='pushd; other-command' and then invoke 'p2 ~/temp' as the shell will return the error: '-bash: pushd: no other directory
– Sarah Soto
Dec 29 '10 at 0:38
You could use a shell function:function p2 () pushd "$@"; other-command;
– Steven D
Dec 29 '10 at 0:46
2
A shell script is a fine way to do this. It's inefficient, but this is not a case where optimization is worth bothering with. Defining a shell function in your~/.bash_profile
(or whatever your shell uses, if not Bash) will also work. Your choice.
– Warren Young
Dec 29 '10 at 3:10
|
show 2 more comments
What I really want is execute 'pushd <params>' followed by another command that takes no params. I may not need a shell script for it - what's a good way to do this?
– Sarah Soto
Dec 29 '10 at 0:23
@sarah-soto All you need then ispushd <params>; other-command
– Shawn J. Goff
Dec 29 '10 at 0:26
I should have clarified: I want a shortcut so I am invoking both commands one after the after without implicitly calling both. I can't aliase the two commands to be alias p2='pushd; other-command' and then invoke 'p2 ~/temp' as the shell will return the error: '-bash: pushd: no other directory
– Sarah Soto
Dec 29 '10 at 0:38
You could use a shell function:function p2 () pushd "$@"; other-command;
– Steven D
Dec 29 '10 at 0:46
2
A shell script is a fine way to do this. It's inefficient, but this is not a case where optimization is worth bothering with. Defining a shell function in your~/.bash_profile
(or whatever your shell uses, if not Bash) will also work. Your choice.
– Warren Young
Dec 29 '10 at 3:10
What I really want is execute 'pushd <params>' followed by another command that takes no params. I may not need a shell script for it - what's a good way to do this?
– Sarah Soto
Dec 29 '10 at 0:23
What I really want is execute 'pushd <params>' followed by another command that takes no params. I may not need a shell script for it - what's a good way to do this?
– Sarah Soto
Dec 29 '10 at 0:23
@sarah-soto All you need then is
pushd <params>; other-command
– Shawn J. Goff
Dec 29 '10 at 0:26
@sarah-soto All you need then is
pushd <params>; other-command
– Shawn J. Goff
Dec 29 '10 at 0:26
I should have clarified: I want a shortcut so I am invoking both commands one after the after without implicitly calling both. I can't aliase the two commands to be alias p2='pushd; other-command' and then invoke 'p2 ~/temp' as the shell will return the error: '-bash: pushd: no other directory
– Sarah Soto
Dec 29 '10 at 0:38
I should have clarified: I want a shortcut so I am invoking both commands one after the after without implicitly calling both. I can't aliase the two commands to be alias p2='pushd; other-command' and then invoke 'p2 ~/temp' as the shell will return the error: '-bash: pushd: no other directory
– Sarah Soto
Dec 29 '10 at 0:38
You could use a shell function:
function p2 () pushd "$@"; other-command;
– Steven D
Dec 29 '10 at 0:46
You could use a shell function:
function p2 () pushd "$@"; other-command;
– Steven D
Dec 29 '10 at 0:46
2
2
A shell script is a fine way to do this. It's inefficient, but this is not a case where optimization is worth bothering with. Defining a shell function in your
~/.bash_profile
(or whatever your shell uses, if not Bash) will also work. Your choice.– Warren Young
Dec 29 '10 at 3:10
A shell script is a fine way to do this. It's inefficient, but this is not a case where optimization is worth bothering with. Defining a shell function in your
~/.bash_profile
(or whatever your shell uses, if not Bash) will also work. Your choice.– Warren Young
Dec 29 '10 at 3:10
|
show 2 more comments
Scripts cannot alter their parent processe's environment. Because of this, any environment changes made in the script are lost.
To run the script in the same process, you can 'source' the script like this
. /path/to/script.sh args
add a comment |
Scripts cannot alter their parent processe's environment. Because of this, any environment changes made in the script are lost.
To run the script in the same process, you can 'source' the script like this
. /path/to/script.sh args
add a comment |
Scripts cannot alter their parent processe's environment. Because of this, any environment changes made in the script are lost.
To run the script in the same process, you can 'source' the script like this
. /path/to/script.sh args
Scripts cannot alter their parent processe's environment. Because of this, any environment changes made in the script are lost.
To run the script in the same process, you can 'source' the script like this
. /path/to/script.sh args
edited Dec 29 '10 at 0:24
answered Dec 28 '10 at 22:56
Shawn J. GoffShawn J. Goff
30k19112134
30k19112134
add a comment |
add a comment |
Instead of a shell script, function is more appropriate for this kind of operation, especially when using pushd.
Add this in .bashrc
:
foo()
pushd $1
time
export foo
In shell:
$ foo mydir1
output:
~/mydir1 ~
real 0m0.000s
user 0m0.000s
sys 0m0.000s
New contributor
add a comment |
Instead of a shell script, function is more appropriate for this kind of operation, especially when using pushd.
Add this in .bashrc
:
foo()
pushd $1
time
export foo
In shell:
$ foo mydir1
output:
~/mydir1 ~
real 0m0.000s
user 0m0.000s
sys 0m0.000s
New contributor
add a comment |
Instead of a shell script, function is more appropriate for this kind of operation, especially when using pushd.
Add this in .bashrc
:
foo()
pushd $1
time
export foo
In shell:
$ foo mydir1
output:
~/mydir1 ~
real 0m0.000s
user 0m0.000s
sys 0m0.000s
New contributor
Instead of a shell script, function is more appropriate for this kind of operation, especially when using pushd.
Add this in .bashrc
:
foo()
pushd $1
time
export foo
In shell:
$ foo mydir1
output:
~/mydir1 ~
real 0m0.000s
user 0m0.000s
sys 0m0.000s
New contributor
New contributor
answered 5 hours ago
otter.prootter.pro
11
11
New contributor
New contributor
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.
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%2f5204%2finvoking-pushd-in-a-shell-script%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
Related question: unix.stackexchange.com/q/2976/250
– phunehehe
Dec 29 '10 at 0:49