Run another command before running the command the user wants to run
Let's say I want to modify the original behavior of the ls
tool this way:
$ ls
Hello World
file1 file2 ...
How can I do this?
When running ls
I would like to run another command let's say echo "Hello World!"
.
The quick solution I see is using alias:
alias orig_ls="ls"
alias ls='echo "Hello World!"'
However, this is not a real solution since when I will run orig_ls
it will output "Hello World!"
.
bash shell command-line alias
add a comment |
Let's say I want to modify the original behavior of the ls
tool this way:
$ ls
Hello World
file1 file2 ...
How can I do this?
When running ls
I would like to run another command let's say echo "Hello World!"
.
The quick solution I see is using alias:
alias orig_ls="ls"
alias ls='echo "Hello World!"'
However, this is not a real solution since when I will run orig_ls
it will output "Hello World!"
.
bash shell command-line alias
Put another ls in the users own path and use this to hook the original.
– peterh
Jan 25 '15 at 14:38
add a comment |
Let's say I want to modify the original behavior of the ls
tool this way:
$ ls
Hello World
file1 file2 ...
How can I do this?
When running ls
I would like to run another command let's say echo "Hello World!"
.
The quick solution I see is using alias:
alias orig_ls="ls"
alias ls='echo "Hello World!"'
However, this is not a real solution since when I will run orig_ls
it will output "Hello World!"
.
bash shell command-line alias
Let's say I want to modify the original behavior of the ls
tool this way:
$ ls
Hello World
file1 file2 ...
How can I do this?
When running ls
I would like to run another command let's say echo "Hello World!"
.
The quick solution I see is using alias:
alias orig_ls="ls"
alias ls='echo "Hello World!"'
However, this is not a real solution since when I will run orig_ls
it will output "Hello World!"
.
bash shell command-line alias
bash shell command-line alias
asked Jan 25 '15 at 14:33
Ionică BizăuIonică Bizău
1,16752040
1,16752040
Put another ls in the users own path and use this to hook the original.
– peterh
Jan 25 '15 at 14:38
add a comment |
Put another ls in the users own path and use this to hook the original.
– peterh
Jan 25 '15 at 14:38
Put another ls in the users own path and use this to hook the original.
– peterh
Jan 25 '15 at 14:38
Put another ls in the users own path and use this to hook the original.
– peterh
Jan 25 '15 at 14:38
add a comment |
2 Answers
2
active
oldest
votes
Sometimes an alias isn't powerful enough to easily do what you want, so here's a way without using them.
In some file that is sourced when your shell starts (e.g. .bashrc
), add the following function:
ls () {
echo "Hello world!"
command ls "$@"
}
Unlike an alias, a function can recurse. That's why command ls
is used instead of ls
; it tells your shell to use the actual ls
instead of the function you've just defined.
2
Indeed. This solution scales better. +1
– PythonNut
Jan 26 '15 at 0:57
Indeed! This is a lot better. Thanks!
– Ionică Bizău
Jan 26 '15 at 7:43
What is thecommand
alternative for OS X?
– Ionică Bizău
Feb 9 '15 at 16:21
@IonicăBizău I have no ideacommand
didn't work :/ I have no idea and I don't have a Mac with which to test it, sorry.
– undergroundmonorail
Feb 9 '15 at 16:25
add a comment |
You must not forget to call ls
:
alias ls='echo "Hello World!"; ls'
Ha! I supposed this will create an infinite cycle! But why doesn't it create an infinite loop?
– Ionică Bizău
Jan 25 '15 at 16:58
1
You might instead try alias ls 'echo "Hello World!"; /usr/bin/ls'
– jamesqf
Jan 25 '15 at 18:19
5
@IonicăBizău any given "alias expansion rule" is only used once.
– David Z
Jan 25 '15 at 19:53
Would joining the two with an & also work? That would also ensure that the user's command is only run if the custom one works, right?
– Nic Hartley
Jan 25 '15 at 20:42
@QPaysTaxes You can use&
indeed but I don't get the argument you make from that.
– Hauke Laging
Jan 26 '15 at 1:02
|
show 2 more comments
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%2f180982%2frun-another-command-before-running-the-command-the-user-wants-to-run%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
Sometimes an alias isn't powerful enough to easily do what you want, so here's a way without using them.
In some file that is sourced when your shell starts (e.g. .bashrc
), add the following function:
ls () {
echo "Hello world!"
command ls "$@"
}
Unlike an alias, a function can recurse. That's why command ls
is used instead of ls
; it tells your shell to use the actual ls
instead of the function you've just defined.
2
Indeed. This solution scales better. +1
– PythonNut
Jan 26 '15 at 0:57
Indeed! This is a lot better. Thanks!
– Ionică Bizău
Jan 26 '15 at 7:43
What is thecommand
alternative for OS X?
– Ionică Bizău
Feb 9 '15 at 16:21
@IonicăBizău I have no ideacommand
didn't work :/ I have no idea and I don't have a Mac with which to test it, sorry.
– undergroundmonorail
Feb 9 '15 at 16:25
add a comment |
Sometimes an alias isn't powerful enough to easily do what you want, so here's a way without using them.
In some file that is sourced when your shell starts (e.g. .bashrc
), add the following function:
ls () {
echo "Hello world!"
command ls "$@"
}
Unlike an alias, a function can recurse. That's why command ls
is used instead of ls
; it tells your shell to use the actual ls
instead of the function you've just defined.
2
Indeed. This solution scales better. +1
– PythonNut
Jan 26 '15 at 0:57
Indeed! This is a lot better. Thanks!
– Ionică Bizău
Jan 26 '15 at 7:43
What is thecommand
alternative for OS X?
– Ionică Bizău
Feb 9 '15 at 16:21
@IonicăBizău I have no ideacommand
didn't work :/ I have no idea and I don't have a Mac with which to test it, sorry.
– undergroundmonorail
Feb 9 '15 at 16:25
add a comment |
Sometimes an alias isn't powerful enough to easily do what you want, so here's a way without using them.
In some file that is sourced when your shell starts (e.g. .bashrc
), add the following function:
ls () {
echo "Hello world!"
command ls "$@"
}
Unlike an alias, a function can recurse. That's why command ls
is used instead of ls
; it tells your shell to use the actual ls
instead of the function you've just defined.
Sometimes an alias isn't powerful enough to easily do what you want, so here's a way without using them.
In some file that is sourced when your shell starts (e.g. .bashrc
), add the following function:
ls () {
echo "Hello world!"
command ls "$@"
}
Unlike an alias, a function can recurse. That's why command ls
is used instead of ls
; it tells your shell to use the actual ls
instead of the function you've just defined.
edited Jan 9 at 23:02
Gilles
532k12810661592
532k12810661592
answered Jan 26 '15 at 0:44
undergroundmonorailundergroundmonorail
701613
701613
2
Indeed. This solution scales better. +1
– PythonNut
Jan 26 '15 at 0:57
Indeed! This is a lot better. Thanks!
– Ionică Bizău
Jan 26 '15 at 7:43
What is thecommand
alternative for OS X?
– Ionică Bizău
Feb 9 '15 at 16:21
@IonicăBizău I have no ideacommand
didn't work :/ I have no idea and I don't have a Mac with which to test it, sorry.
– undergroundmonorail
Feb 9 '15 at 16:25
add a comment |
2
Indeed. This solution scales better. +1
– PythonNut
Jan 26 '15 at 0:57
Indeed! This is a lot better. Thanks!
– Ionică Bizău
Jan 26 '15 at 7:43
What is thecommand
alternative for OS X?
– Ionică Bizău
Feb 9 '15 at 16:21
@IonicăBizău I have no ideacommand
didn't work :/ I have no idea and I don't have a Mac with which to test it, sorry.
– undergroundmonorail
Feb 9 '15 at 16:25
2
2
Indeed. This solution scales better. +1
– PythonNut
Jan 26 '15 at 0:57
Indeed. This solution scales better. +1
– PythonNut
Jan 26 '15 at 0:57
Indeed! This is a lot better. Thanks!
– Ionică Bizău
Jan 26 '15 at 7:43
Indeed! This is a lot better. Thanks!
– Ionică Bizău
Jan 26 '15 at 7:43
What is the
command
alternative for OS X?– Ionică Bizău
Feb 9 '15 at 16:21
What is the
command
alternative for OS X?– Ionică Bizău
Feb 9 '15 at 16:21
@IonicăBizău I have no idea
command
didn't work :/ I have no idea and I don't have a Mac with which to test it, sorry.– undergroundmonorail
Feb 9 '15 at 16:25
@IonicăBizău I have no idea
command
didn't work :/ I have no idea and I don't have a Mac with which to test it, sorry.– undergroundmonorail
Feb 9 '15 at 16:25
add a comment |
You must not forget to call ls
:
alias ls='echo "Hello World!"; ls'
Ha! I supposed this will create an infinite cycle! But why doesn't it create an infinite loop?
– Ionică Bizău
Jan 25 '15 at 16:58
1
You might instead try alias ls 'echo "Hello World!"; /usr/bin/ls'
– jamesqf
Jan 25 '15 at 18:19
5
@IonicăBizău any given "alias expansion rule" is only used once.
– David Z
Jan 25 '15 at 19:53
Would joining the two with an & also work? That would also ensure that the user's command is only run if the custom one works, right?
– Nic Hartley
Jan 25 '15 at 20:42
@QPaysTaxes You can use&
indeed but I don't get the argument you make from that.
– Hauke Laging
Jan 26 '15 at 1:02
|
show 2 more comments
You must not forget to call ls
:
alias ls='echo "Hello World!"; ls'
Ha! I supposed this will create an infinite cycle! But why doesn't it create an infinite loop?
– Ionică Bizău
Jan 25 '15 at 16:58
1
You might instead try alias ls 'echo "Hello World!"; /usr/bin/ls'
– jamesqf
Jan 25 '15 at 18:19
5
@IonicăBizău any given "alias expansion rule" is only used once.
– David Z
Jan 25 '15 at 19:53
Would joining the two with an & also work? That would also ensure that the user's command is only run if the custom one works, right?
– Nic Hartley
Jan 25 '15 at 20:42
@QPaysTaxes You can use&
indeed but I don't get the argument you make from that.
– Hauke Laging
Jan 26 '15 at 1:02
|
show 2 more comments
You must not forget to call ls
:
alias ls='echo "Hello World!"; ls'
You must not forget to call ls
:
alias ls='echo "Hello World!"; ls'
answered Jan 25 '15 at 14:38
Hauke LagingHauke Laging
56.2k1285135
56.2k1285135
Ha! I supposed this will create an infinite cycle! But why doesn't it create an infinite loop?
– Ionică Bizău
Jan 25 '15 at 16:58
1
You might instead try alias ls 'echo "Hello World!"; /usr/bin/ls'
– jamesqf
Jan 25 '15 at 18:19
5
@IonicăBizău any given "alias expansion rule" is only used once.
– David Z
Jan 25 '15 at 19:53
Would joining the two with an & also work? That would also ensure that the user's command is only run if the custom one works, right?
– Nic Hartley
Jan 25 '15 at 20:42
@QPaysTaxes You can use&
indeed but I don't get the argument you make from that.
– Hauke Laging
Jan 26 '15 at 1:02
|
show 2 more comments
Ha! I supposed this will create an infinite cycle! But why doesn't it create an infinite loop?
– Ionică Bizău
Jan 25 '15 at 16:58
1
You might instead try alias ls 'echo "Hello World!"; /usr/bin/ls'
– jamesqf
Jan 25 '15 at 18:19
5
@IonicăBizău any given "alias expansion rule" is only used once.
– David Z
Jan 25 '15 at 19:53
Would joining the two with an & also work? That would also ensure that the user's command is only run if the custom one works, right?
– Nic Hartley
Jan 25 '15 at 20:42
@QPaysTaxes You can use&
indeed but I don't get the argument you make from that.
– Hauke Laging
Jan 26 '15 at 1:02
Ha! I supposed this will create an infinite cycle! But why doesn't it create an infinite loop?
– Ionică Bizău
Jan 25 '15 at 16:58
Ha! I supposed this will create an infinite cycle! But why doesn't it create an infinite loop?
– Ionică Bizău
Jan 25 '15 at 16:58
1
1
You might instead try alias ls 'echo "Hello World!"; /usr/bin/ls'
– jamesqf
Jan 25 '15 at 18:19
You might instead try alias ls 'echo "Hello World!"; /usr/bin/ls'
– jamesqf
Jan 25 '15 at 18:19
5
5
@IonicăBizău any given "alias expansion rule" is only used once.
– David Z
Jan 25 '15 at 19:53
@IonicăBizău any given "alias expansion rule" is only used once.
– David Z
Jan 25 '15 at 19:53
Would joining the two with an & also work? That would also ensure that the user's command is only run if the custom one works, right?
– Nic Hartley
Jan 25 '15 at 20:42
Would joining the two with an & also work? That would also ensure that the user's command is only run if the custom one works, right?
– Nic Hartley
Jan 25 '15 at 20:42
@QPaysTaxes You can use
&
indeed but I don't get the argument you make from that.– Hauke Laging
Jan 26 '15 at 1:02
@QPaysTaxes You can use
&
indeed but I don't get the argument you make from that.– Hauke Laging
Jan 26 '15 at 1:02
|
show 2 more comments
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%2f180982%2frun-another-command-before-running-the-command-the-user-wants-to-run%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
Put another ls in the users own path and use this to hook the original.
– peterh
Jan 25 '15 at 14:38