alias or bash function does not workHow to get awk to do regex on column variableMy alias parser works on the command line but not from a scriptHow do you wrap executable commands so that they work in an alias or function?In Bash, when to alias, when to script, and when to write a function?Alias in .bashrc doesn't seem to accept an argumentWhy alias inside function does not work?Single or Double quotes when defining an alias?Outputting PID of Rails server(s) with ps aux | grep - breaks when put into an alias?How to make a customized function in bash fileFile creation won't take place from within a function in a sourced fileThe alias does not work ?
Why doesn't Newton's third law mean a person bounces back to where they started when they hit the ground?
Example of a relative pronoun
Circuitry of TV splitters
A function which translates a sentence to title-case
What Brexit solution does the DUP want?
Download, install and reboot computer at night if needed
What is the command to reset a PC without deleting any files
Non-Jewish family in an Orthodox Jewish Wedding
Is there really no realistic way for a skeleton monster to move around without magic?
What do you call something that goes against the spirit of the law, but is legal when interpreting the law to the letter?
Why did the Germans forbid the possession of pet pigeons in Rostov-on-Don in 1941?
Chess with symmetric move-square
Extreme, but not acceptable situation and I can't start the work tomorrow morning
declaring a variable twice in IIFE
What is the meaning of "of trouble" in the following sentence?
Should I join an office cleaning event for free?
Are white and non-white police officers equally likely to kill black suspects?
N.B. ligature in Latex
Is it legal to have the "// (c) 2019 John Smith" header in all files when there are hundreds of contributors?
Finding files for which a command fails
Concept of linear mappings are confusing me
Accidentally leaked the solution to an assignment, what to do now? (I'm the prof)
If Manufacturer spice model and Datasheet give different values which should I use?
Is there a familial term for apples and pears?
alias or bash function does not work
How to get awk to do regex on column variableMy alias parser works on the command line but not from a scriptHow do you wrap executable commands so that they work in an alias or function?In Bash, when to alias, when to script, and when to write a function?Alias in .bashrc doesn't seem to accept an argumentWhy alias inside function does not work?Single or Double quotes when defining an alias?Outputting PID of Rails server(s) with ps aux | grep - breaks when put into an alias?How to make a customized function in bash fileFile creation won't take place from within a function in a sourced fileThe alias does not work ?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
When I create
alias wd='ps -ef | grep java | awk 'print $2 " " $9' | egrep "(A|B|C|D)"'
or
function wd () awk 'print $2' ...
in my .bashrc file, I get errors. Interestingly, if I source my .bashrc file with the function, it 'compiles', but when executing, gives me:
context is
>>> <<< print
missing
Can someone help me with this, and also answer when its better to put something in a function versus in an alias?
bash alias function
add a comment |
When I create
alias wd='ps -ef | grep java | awk 'print $2 " " $9' | egrep "(A|B|C|D)"'
or
function wd () awk 'print $2' ...
in my .bashrc file, I get errors. Interestingly, if I source my .bashrc file with the function, it 'compiles', but when executing, gives me:
context is
>>> <<< print
missing
Can someone help me with this, and also answer when its better to put something in a function versus in an alias?
bash alias function
add a comment |
When I create
alias wd='ps -ef | grep java | awk 'print $2 " " $9' | egrep "(A|B|C|D)"'
or
function wd () awk 'print $2' ...
in my .bashrc file, I get errors. Interestingly, if I source my .bashrc file with the function, it 'compiles', but when executing, gives me:
context is
>>> <<< print
missing
Can someone help me with this, and also answer when its better to put something in a function versus in an alias?
bash alias function
When I create
alias wd='ps -ef | grep java | awk 'print $2 " " $9' | egrep "(A|B|C|D)"'
or
function wd () awk 'print $2' ...
in my .bashrc file, I get errors. Interestingly, if I source my .bashrc file with the function, it 'compiles', but when executing, gives me:
context is
>>> <<< print
missing
Can someone help me with this, and also answer when its better to put something in a function versus in an alias?
bash alias function
bash alias function
asked Mar 18 '11 at 18:23
Amir AfghaniAmir Afghani
2,37892020
2,37892020
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Why the alias doesn't work
alias wd='ps -ef | grep java | awk 'print $2 " " $9' | egrep "(A|B|C|D)"'
The alias
command receives three arguments. The first is the string wd=ps -ef | grep java | awk print
(the single quotes prevent the characters between them from having a special meaning). The second argument consists of a single space character. (In .bashrc
, the positional parameters $2
and $9
are empty, so $2
expands to a list of 0 words.) The third argument is | egrep "(A|B|C|D)"
(again the single quotes protect the special characters).
The alias definition is parsed like any other shell command when it is encountered. Then the string defined for the alias is parsed when the alias is expanded. Here are some possible ways to define this alias. First possibility: since the whole alias definition is within single quotes, only use double quotes in the commands, which means you must protect the "
and $
meant for awk with backslashes.
alias wd='ps -ef | grep java | awk "print $2 " " $9" | egrep "(A|B|C|D)"'
Second possibility: every character stands for itself within single quotes, except that a single quote ends the literal string. '''
is an idiom for “single quote inside a single-quoted string”: end the single-quoted string, put a literal single quote, and immediately start a new single-quoted string. Since there's no intervening space, it's still the same word.
alias wd='ps -ef | grep java | awk '''print $2 " " $9''' | egrep "(A|B|C|D)"'
You can simplify this a bit:
alias wd='ps -ef | grep java | awk '''print $2, $9''' | egrep "(A|B|C|D)"'
Tip: use set -x
to see how the shell is expanding your commands.
Why the function doesn't work
I don't know. The part you show looks ok. If you still don't understand why your function isn't working after my explanations, copy-paste your code.
Alias or function?
Use an alias only for very simple things, typically to give a shorter name to a frequently-used command or provide default options. Examples:
alias grep='grep --color'
alias cp='cp -i'
alias j=jobs
For anything more complicated, use functions.
What you should have written
Instead of parsing the ps
output, make it generate output that suits you.
wd ()
ps -C java -o pid=,cmd=
add a comment |
The problem with the alias is that quotes don't nest directly (except, as a special case, inside $()
). You need to escape the inner ones.
alias wd='ps -ef | grep java | awk 'print $2 " " $9' | egrep "(A|B|C|D)"'
# ^^ ^^
You've removed too much of the function form for me to be certain, but the error snippet is from awk
and suggests quoting or shell variable expansion problems.
As a general rule, functions are more flexible than aliases (you have no control over argument processing with aliases aside from history expansion), but aliases are a little faster, and if you end the alias with a space then the first argument gets expanded as a command (tab completion, etc.) Aliases also don't work in the file they're declared in (to avoid infinite alias expansion loops).
1
Several errors here. Your quoting doesn't work at all: backslashes are not special inside single quotes. (See my answer if you need more explanation.) An alias ending with a space has no influence on completion in bash (I think). Aliases do work as soon as they are declared (or in bash, maybe only on the next line, but that's a bug); alias expansion loops are prevented by a completely different mechanism (an aliasfoo
is not expanded in the (direct or indirect) result of the expansion offoo
).
– Gilles
Mar 18 '11 at 19:20
add a comment |
I dont think you can pass an argument to an alias. An alias is just a string replacement rule for the first word of a command.
Example:
alias wd='ps -ef | grep java | awk 'print $2 " " $9' | egrep "(A|B|C|D)"'
will result in the command wd arg1 arg2 arg3
being replaced and executed as
ps -ef | grep java | awk 'print " " ' | egrep "(A|B|C|D)" arg1 arg2 arg3
For everything beyond that, use functions.
Actually, because$2
and$9
are inside single quotes, they won't be expanded by the shell but byawk
, which uses them as field selectors for theps
output: the uid and the first argument to the command, for Linuxps -f
. That said, the failed nested quoting means the shell was expanding them when it shouldn't have been.
– geekosaur
Mar 18 '11 at 18:53
@geekosaur oh, yes, of course. I haven't looked at it that way.
– artistoex
Mar 18 '11 at 18:58
@geekosaur, @artistoex: Yes, you can pass an argument to an alias, and in fact it's commonly used. Here,$2
and$9
are unprotected when the alias is defined. See my answer for the full story.
– Gilles
Mar 18 '11 at 19:22
@gilles The argument is not passed to the alias in the sense you can not effect where the original arguments will be placed in the final command string. It will always be placed at the end.
– artistoex
Mar 19 '11 at 10:30
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%2f9572%2falias-or-bash-function-does-not-work%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
Why the alias doesn't work
alias wd='ps -ef | grep java | awk 'print $2 " " $9' | egrep "(A|B|C|D)"'
The alias
command receives three arguments. The first is the string wd=ps -ef | grep java | awk print
(the single quotes prevent the characters between them from having a special meaning). The second argument consists of a single space character. (In .bashrc
, the positional parameters $2
and $9
are empty, so $2
expands to a list of 0 words.) The third argument is | egrep "(A|B|C|D)"
(again the single quotes protect the special characters).
The alias definition is parsed like any other shell command when it is encountered. Then the string defined for the alias is parsed when the alias is expanded. Here are some possible ways to define this alias. First possibility: since the whole alias definition is within single quotes, only use double quotes in the commands, which means you must protect the "
and $
meant for awk with backslashes.
alias wd='ps -ef | grep java | awk "print $2 " " $9" | egrep "(A|B|C|D)"'
Second possibility: every character stands for itself within single quotes, except that a single quote ends the literal string. '''
is an idiom for “single quote inside a single-quoted string”: end the single-quoted string, put a literal single quote, and immediately start a new single-quoted string. Since there's no intervening space, it's still the same word.
alias wd='ps -ef | grep java | awk '''print $2 " " $9''' | egrep "(A|B|C|D)"'
You can simplify this a bit:
alias wd='ps -ef | grep java | awk '''print $2, $9''' | egrep "(A|B|C|D)"'
Tip: use set -x
to see how the shell is expanding your commands.
Why the function doesn't work
I don't know. The part you show looks ok. If you still don't understand why your function isn't working after my explanations, copy-paste your code.
Alias or function?
Use an alias only for very simple things, typically to give a shorter name to a frequently-used command or provide default options. Examples:
alias grep='grep --color'
alias cp='cp -i'
alias j=jobs
For anything more complicated, use functions.
What you should have written
Instead of parsing the ps
output, make it generate output that suits you.
wd ()
ps -C java -o pid=,cmd=
add a comment |
Why the alias doesn't work
alias wd='ps -ef | grep java | awk 'print $2 " " $9' | egrep "(A|B|C|D)"'
The alias
command receives three arguments. The first is the string wd=ps -ef | grep java | awk print
(the single quotes prevent the characters between them from having a special meaning). The second argument consists of a single space character. (In .bashrc
, the positional parameters $2
and $9
are empty, so $2
expands to a list of 0 words.) The third argument is | egrep "(A|B|C|D)"
(again the single quotes protect the special characters).
The alias definition is parsed like any other shell command when it is encountered. Then the string defined for the alias is parsed when the alias is expanded. Here are some possible ways to define this alias. First possibility: since the whole alias definition is within single quotes, only use double quotes in the commands, which means you must protect the "
and $
meant for awk with backslashes.
alias wd='ps -ef | grep java | awk "print $2 " " $9" | egrep "(A|B|C|D)"'
Second possibility: every character stands for itself within single quotes, except that a single quote ends the literal string. '''
is an idiom for “single quote inside a single-quoted string”: end the single-quoted string, put a literal single quote, and immediately start a new single-quoted string. Since there's no intervening space, it's still the same word.
alias wd='ps -ef | grep java | awk '''print $2 " " $9''' | egrep "(A|B|C|D)"'
You can simplify this a bit:
alias wd='ps -ef | grep java | awk '''print $2, $9''' | egrep "(A|B|C|D)"'
Tip: use set -x
to see how the shell is expanding your commands.
Why the function doesn't work
I don't know. The part you show looks ok. If you still don't understand why your function isn't working after my explanations, copy-paste your code.
Alias or function?
Use an alias only for very simple things, typically to give a shorter name to a frequently-used command or provide default options. Examples:
alias grep='grep --color'
alias cp='cp -i'
alias j=jobs
For anything more complicated, use functions.
What you should have written
Instead of parsing the ps
output, make it generate output that suits you.
wd ()
ps -C java -o pid=,cmd=
add a comment |
Why the alias doesn't work
alias wd='ps -ef | grep java | awk 'print $2 " " $9' | egrep "(A|B|C|D)"'
The alias
command receives three arguments. The first is the string wd=ps -ef | grep java | awk print
(the single quotes prevent the characters between them from having a special meaning). The second argument consists of a single space character. (In .bashrc
, the positional parameters $2
and $9
are empty, so $2
expands to a list of 0 words.) The third argument is | egrep "(A|B|C|D)"
(again the single quotes protect the special characters).
The alias definition is parsed like any other shell command when it is encountered. Then the string defined for the alias is parsed when the alias is expanded. Here are some possible ways to define this alias. First possibility: since the whole alias definition is within single quotes, only use double quotes in the commands, which means you must protect the "
and $
meant for awk with backslashes.
alias wd='ps -ef | grep java | awk "print $2 " " $9" | egrep "(A|B|C|D)"'
Second possibility: every character stands for itself within single quotes, except that a single quote ends the literal string. '''
is an idiom for “single quote inside a single-quoted string”: end the single-quoted string, put a literal single quote, and immediately start a new single-quoted string. Since there's no intervening space, it's still the same word.
alias wd='ps -ef | grep java | awk '''print $2 " " $9''' | egrep "(A|B|C|D)"'
You can simplify this a bit:
alias wd='ps -ef | grep java | awk '''print $2, $9''' | egrep "(A|B|C|D)"'
Tip: use set -x
to see how the shell is expanding your commands.
Why the function doesn't work
I don't know. The part you show looks ok. If you still don't understand why your function isn't working after my explanations, copy-paste your code.
Alias or function?
Use an alias only for very simple things, typically to give a shorter name to a frequently-used command or provide default options. Examples:
alias grep='grep --color'
alias cp='cp -i'
alias j=jobs
For anything more complicated, use functions.
What you should have written
Instead of parsing the ps
output, make it generate output that suits you.
wd ()
ps -C java -o pid=,cmd=
Why the alias doesn't work
alias wd='ps -ef | grep java | awk 'print $2 " " $9' | egrep "(A|B|C|D)"'
The alias
command receives three arguments. The first is the string wd=ps -ef | grep java | awk print
(the single quotes prevent the characters between them from having a special meaning). The second argument consists of a single space character. (In .bashrc
, the positional parameters $2
and $9
are empty, so $2
expands to a list of 0 words.) The third argument is | egrep "(A|B|C|D)"
(again the single quotes protect the special characters).
The alias definition is parsed like any other shell command when it is encountered. Then the string defined for the alias is parsed when the alias is expanded. Here are some possible ways to define this alias. First possibility: since the whole alias definition is within single quotes, only use double quotes in the commands, which means you must protect the "
and $
meant for awk with backslashes.
alias wd='ps -ef | grep java | awk "print $2 " " $9" | egrep "(A|B|C|D)"'
Second possibility: every character stands for itself within single quotes, except that a single quote ends the literal string. '''
is an idiom for “single quote inside a single-quoted string”: end the single-quoted string, put a literal single quote, and immediately start a new single-quoted string. Since there's no intervening space, it's still the same word.
alias wd='ps -ef | grep java | awk '''print $2 " " $9''' | egrep "(A|B|C|D)"'
You can simplify this a bit:
alias wd='ps -ef | grep java | awk '''print $2, $9''' | egrep "(A|B|C|D)"'
Tip: use set -x
to see how the shell is expanding your commands.
Why the function doesn't work
I don't know. The part you show looks ok. If you still don't understand why your function isn't working after my explanations, copy-paste your code.
Alias or function?
Use an alias only for very simple things, typically to give a shorter name to a frequently-used command or provide default options. Examples:
alias grep='grep --color'
alias cp='cp -i'
alias j=jobs
For anything more complicated, use functions.
What you should have written
Instead of parsing the ps
output, make it generate output that suits you.
wd ()
ps -C java -o pid=,cmd=
answered Mar 18 '11 at 19:16
GillesGilles
546k12911111625
546k12911111625
add a comment |
add a comment |
The problem with the alias is that quotes don't nest directly (except, as a special case, inside $()
). You need to escape the inner ones.
alias wd='ps -ef | grep java | awk 'print $2 " " $9' | egrep "(A|B|C|D)"'
# ^^ ^^
You've removed too much of the function form for me to be certain, but the error snippet is from awk
and suggests quoting or shell variable expansion problems.
As a general rule, functions are more flexible than aliases (you have no control over argument processing with aliases aside from history expansion), but aliases are a little faster, and if you end the alias with a space then the first argument gets expanded as a command (tab completion, etc.) Aliases also don't work in the file they're declared in (to avoid infinite alias expansion loops).
1
Several errors here. Your quoting doesn't work at all: backslashes are not special inside single quotes. (See my answer if you need more explanation.) An alias ending with a space has no influence on completion in bash (I think). Aliases do work as soon as they are declared (or in bash, maybe only on the next line, but that's a bug); alias expansion loops are prevented by a completely different mechanism (an aliasfoo
is not expanded in the (direct or indirect) result of the expansion offoo
).
– Gilles
Mar 18 '11 at 19:20
add a comment |
The problem with the alias is that quotes don't nest directly (except, as a special case, inside $()
). You need to escape the inner ones.
alias wd='ps -ef | grep java | awk 'print $2 " " $9' | egrep "(A|B|C|D)"'
# ^^ ^^
You've removed too much of the function form for me to be certain, but the error snippet is from awk
and suggests quoting or shell variable expansion problems.
As a general rule, functions are more flexible than aliases (you have no control over argument processing with aliases aside from history expansion), but aliases are a little faster, and if you end the alias with a space then the first argument gets expanded as a command (tab completion, etc.) Aliases also don't work in the file they're declared in (to avoid infinite alias expansion loops).
1
Several errors here. Your quoting doesn't work at all: backslashes are not special inside single quotes. (See my answer if you need more explanation.) An alias ending with a space has no influence on completion in bash (I think). Aliases do work as soon as they are declared (or in bash, maybe only on the next line, but that's a bug); alias expansion loops are prevented by a completely different mechanism (an aliasfoo
is not expanded in the (direct or indirect) result of the expansion offoo
).
– Gilles
Mar 18 '11 at 19:20
add a comment |
The problem with the alias is that quotes don't nest directly (except, as a special case, inside $()
). You need to escape the inner ones.
alias wd='ps -ef | grep java | awk 'print $2 " " $9' | egrep "(A|B|C|D)"'
# ^^ ^^
You've removed too much of the function form for me to be certain, but the error snippet is from awk
and suggests quoting or shell variable expansion problems.
As a general rule, functions are more flexible than aliases (you have no control over argument processing with aliases aside from history expansion), but aliases are a little faster, and if you end the alias with a space then the first argument gets expanded as a command (tab completion, etc.) Aliases also don't work in the file they're declared in (to avoid infinite alias expansion loops).
The problem with the alias is that quotes don't nest directly (except, as a special case, inside $()
). You need to escape the inner ones.
alias wd='ps -ef | grep java | awk 'print $2 " " $9' | egrep "(A|B|C|D)"'
# ^^ ^^
You've removed too much of the function form for me to be certain, but the error snippet is from awk
and suggests quoting or shell variable expansion problems.
As a general rule, functions are more flexible than aliases (you have no control over argument processing with aliases aside from history expansion), but aliases are a little faster, and if you end the alias with a space then the first argument gets expanded as a command (tab completion, etc.) Aliases also don't work in the file they're declared in (to avoid infinite alias expansion loops).
answered Mar 18 '11 at 18:39
geekosaurgeekosaur
23k36053
23k36053
1
Several errors here. Your quoting doesn't work at all: backslashes are not special inside single quotes. (See my answer if you need more explanation.) An alias ending with a space has no influence on completion in bash (I think). Aliases do work as soon as they are declared (or in bash, maybe only on the next line, but that's a bug); alias expansion loops are prevented by a completely different mechanism (an aliasfoo
is not expanded in the (direct or indirect) result of the expansion offoo
).
– Gilles
Mar 18 '11 at 19:20
add a comment |
1
Several errors here. Your quoting doesn't work at all: backslashes are not special inside single quotes. (See my answer if you need more explanation.) An alias ending with a space has no influence on completion in bash (I think). Aliases do work as soon as they are declared (or in bash, maybe only on the next line, but that's a bug); alias expansion loops are prevented by a completely different mechanism (an aliasfoo
is not expanded in the (direct or indirect) result of the expansion offoo
).
– Gilles
Mar 18 '11 at 19:20
1
1
Several errors here. Your quoting doesn't work at all: backslashes are not special inside single quotes. (See my answer if you need more explanation.) An alias ending with a space has no influence on completion in bash (I think). Aliases do work as soon as they are declared (or in bash, maybe only on the next line, but that's a bug); alias expansion loops are prevented by a completely different mechanism (an alias
foo
is not expanded in the (direct or indirect) result of the expansion of foo
).– Gilles
Mar 18 '11 at 19:20
Several errors here. Your quoting doesn't work at all: backslashes are not special inside single quotes. (See my answer if you need more explanation.) An alias ending with a space has no influence on completion in bash (I think). Aliases do work as soon as they are declared (or in bash, maybe only on the next line, but that's a bug); alias expansion loops are prevented by a completely different mechanism (an alias
foo
is not expanded in the (direct or indirect) result of the expansion of foo
).– Gilles
Mar 18 '11 at 19:20
add a comment |
I dont think you can pass an argument to an alias. An alias is just a string replacement rule for the first word of a command.
Example:
alias wd='ps -ef | grep java | awk 'print $2 " " $9' | egrep "(A|B|C|D)"'
will result in the command wd arg1 arg2 arg3
being replaced and executed as
ps -ef | grep java | awk 'print " " ' | egrep "(A|B|C|D)" arg1 arg2 arg3
For everything beyond that, use functions.
Actually, because$2
and$9
are inside single quotes, they won't be expanded by the shell but byawk
, which uses them as field selectors for theps
output: the uid and the first argument to the command, for Linuxps -f
. That said, the failed nested quoting means the shell was expanding them when it shouldn't have been.
– geekosaur
Mar 18 '11 at 18:53
@geekosaur oh, yes, of course. I haven't looked at it that way.
– artistoex
Mar 18 '11 at 18:58
@geekosaur, @artistoex: Yes, you can pass an argument to an alias, and in fact it's commonly used. Here,$2
and$9
are unprotected when the alias is defined. See my answer for the full story.
– Gilles
Mar 18 '11 at 19:22
@gilles The argument is not passed to the alias in the sense you can not effect where the original arguments will be placed in the final command string. It will always be placed at the end.
– artistoex
Mar 19 '11 at 10:30
add a comment |
I dont think you can pass an argument to an alias. An alias is just a string replacement rule for the first word of a command.
Example:
alias wd='ps -ef | grep java | awk 'print $2 " " $9' | egrep "(A|B|C|D)"'
will result in the command wd arg1 arg2 arg3
being replaced and executed as
ps -ef | grep java | awk 'print " " ' | egrep "(A|B|C|D)" arg1 arg2 arg3
For everything beyond that, use functions.
Actually, because$2
and$9
are inside single quotes, they won't be expanded by the shell but byawk
, which uses them as field selectors for theps
output: the uid and the first argument to the command, for Linuxps -f
. That said, the failed nested quoting means the shell was expanding them when it shouldn't have been.
– geekosaur
Mar 18 '11 at 18:53
@geekosaur oh, yes, of course. I haven't looked at it that way.
– artistoex
Mar 18 '11 at 18:58
@geekosaur, @artistoex: Yes, you can pass an argument to an alias, and in fact it's commonly used. Here,$2
and$9
are unprotected when the alias is defined. See my answer for the full story.
– Gilles
Mar 18 '11 at 19:22
@gilles The argument is not passed to the alias in the sense you can not effect where the original arguments will be placed in the final command string. It will always be placed at the end.
– artistoex
Mar 19 '11 at 10:30
add a comment |
I dont think you can pass an argument to an alias. An alias is just a string replacement rule for the first word of a command.
Example:
alias wd='ps -ef | grep java | awk 'print $2 " " $9' | egrep "(A|B|C|D)"'
will result in the command wd arg1 arg2 arg3
being replaced and executed as
ps -ef | grep java | awk 'print " " ' | egrep "(A|B|C|D)" arg1 arg2 arg3
For everything beyond that, use functions.
I dont think you can pass an argument to an alias. An alias is just a string replacement rule for the first word of a command.
Example:
alias wd='ps -ef | grep java | awk 'print $2 " " $9' | egrep "(A|B|C|D)"'
will result in the command wd arg1 arg2 arg3
being replaced and executed as
ps -ef | grep java | awk 'print " " ' | egrep "(A|B|C|D)" arg1 arg2 arg3
For everything beyond that, use functions.
answered Mar 18 '11 at 18:50
artistoexartistoex
994913
994913
Actually, because$2
and$9
are inside single quotes, they won't be expanded by the shell but byawk
, which uses them as field selectors for theps
output: the uid and the first argument to the command, for Linuxps -f
. That said, the failed nested quoting means the shell was expanding them when it shouldn't have been.
– geekosaur
Mar 18 '11 at 18:53
@geekosaur oh, yes, of course. I haven't looked at it that way.
– artistoex
Mar 18 '11 at 18:58
@geekosaur, @artistoex: Yes, you can pass an argument to an alias, and in fact it's commonly used. Here,$2
and$9
are unprotected when the alias is defined. See my answer for the full story.
– Gilles
Mar 18 '11 at 19:22
@gilles The argument is not passed to the alias in the sense you can not effect where the original arguments will be placed in the final command string. It will always be placed at the end.
– artistoex
Mar 19 '11 at 10:30
add a comment |
Actually, because$2
and$9
are inside single quotes, they won't be expanded by the shell but byawk
, which uses them as field selectors for theps
output: the uid and the first argument to the command, for Linuxps -f
. That said, the failed nested quoting means the shell was expanding them when it shouldn't have been.
– geekosaur
Mar 18 '11 at 18:53
@geekosaur oh, yes, of course. I haven't looked at it that way.
– artistoex
Mar 18 '11 at 18:58
@geekosaur, @artistoex: Yes, you can pass an argument to an alias, and in fact it's commonly used. Here,$2
and$9
are unprotected when the alias is defined. See my answer for the full story.
– Gilles
Mar 18 '11 at 19:22
@gilles The argument is not passed to the alias in the sense you can not effect where the original arguments will be placed in the final command string. It will always be placed at the end.
– artistoex
Mar 19 '11 at 10:30
Actually, because
$2
and $9
are inside single quotes, they won't be expanded by the shell but by awk
, which uses them as field selectors for the ps
output: the uid and the first argument to the command, for Linux ps -f
. That said, the failed nested quoting means the shell was expanding them when it shouldn't have been.– geekosaur
Mar 18 '11 at 18:53
Actually, because
$2
and $9
are inside single quotes, they won't be expanded by the shell but by awk
, which uses them as field selectors for the ps
output: the uid and the first argument to the command, for Linux ps -f
. That said, the failed nested quoting means the shell was expanding them when it shouldn't have been.– geekosaur
Mar 18 '11 at 18:53
@geekosaur oh, yes, of course. I haven't looked at it that way.
– artistoex
Mar 18 '11 at 18:58
@geekosaur oh, yes, of course. I haven't looked at it that way.
– artistoex
Mar 18 '11 at 18:58
@geekosaur, @artistoex: Yes, you can pass an argument to an alias, and in fact it's commonly used. Here,
$2
and $9
are unprotected when the alias is defined. See my answer for the full story.– Gilles
Mar 18 '11 at 19:22
@geekosaur, @artistoex: Yes, you can pass an argument to an alias, and in fact it's commonly used. Here,
$2
and $9
are unprotected when the alias is defined. See my answer for the full story.– Gilles
Mar 18 '11 at 19:22
@gilles The argument is not passed to the alias in the sense you can not effect where the original arguments will be placed in the final command string. It will always be placed at the end.
– artistoex
Mar 19 '11 at 10:30
@gilles The argument is not passed to the alias in the sense you can not effect where the original arguments will be placed in the final command string. It will always be placed at the end.
– artistoex
Mar 19 '11 at 10:30
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%2f9572%2falias-or-bash-function-does-not-work%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
-alias, bash, function