$@ in alias inside script: is there a “local” $@?In Bash, when to alias, when to script, and when to...

What is the purpose/function of this power inductor in parallel?

Why do aircraft leave cruising altitude long before landing just to circle?

Ending a line of dialogue with "?!": Allowed or obnoxious?

What is the best way to use errors in mathematica M12?

global variant of csname…endcsname

May the tower use the runway while an emergency aircraft is inbound?

How do the Durable and Dwarven Fortitude feats interact?

What exactly happened to the 18 crew members who were reported as "missing" in "Q Who"?

Vegetarian dishes on Russian trains (European part)

Why was ramjet fuel used as hydraulic fluid during Saturn V checkout?

What's the point of writing that I know will never be used or read?

Reducing contention in thread-safe LruCache

How to render "have ideas above his station" into German

Postdoc interview - somewhat positive reply but no news?

Output with the same length always

Quick destruction of a helium filled airship?

Build a mob of suspiciously happy lenny faces ( ͡° ͜ʖ ͡°)

Replacing old plug-in 220V range with new hardwire 3-wire electric cooktop: remove outlet or add a plug?

Compute the square root of a positive integer using binary search

Why do so many people play out of turn on the last lead?

Subgroup generated by a subgroup and a conjugate of it

Heyawacky: Ace of Cups

Are there any OR challenges that are similar to kaggle's competitions?

Existence of a certain set of 0/1-sequences without the Axiom of Choice



$@ in alias inside script: is there a “local” $@?


In Bash, when to alias, when to script, and when to write a function?Tab completion doesn't work for arguments when command is an aliasfunctions argumentsaliasing command which takes argument with using pipeRun “nohup $COMMAND > /dev/null &” with “back $command”Make “.” and “source” default to ~/.zshrcWhich of the following shell operations are performed inside the function body when running a function definition and when calling a function?Bash functions, something strange going on!How to make my bash function known to external program






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







0















I have aliased pushd in my bash shell as follows so that it suppresses output:



alias pushd='pushd "$@" > /dev/null'


This works fine most of the time, but I'm running into trouble now using it inside functions that take arguments. For example,



test() {
pushd .
...
}


Running test without arguments is fine. But with arguments:



> test x y z
bash: pushd: too many arguments


I take it that pushd is trying to take . x y z as arguments instead of just .. How can I prevent this? Is there a "local" equivalent of $@ that would only see . and not x y z?










share|improve this question









New contributor



Théophile is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

















  • 1





    Why are you using $@ at all?

    – Wildcard
    2 days ago











  • @Wildcard Because I had copy/pasted that line from someone else who was apparently also a beginner like me. :S

    – Théophile
    yesterday


















0















I have aliased pushd in my bash shell as follows so that it suppresses output:



alias pushd='pushd "$@" > /dev/null'


This works fine most of the time, but I'm running into trouble now using it inside functions that take arguments. For example,



test() {
pushd .
...
}


Running test without arguments is fine. But with arguments:



> test x y z
bash: pushd: too many arguments


I take it that pushd is trying to take . x y z as arguments instead of just .. How can I prevent this? Is there a "local" equivalent of $@ that would only see . and not x y z?










share|improve this question









New contributor



Théophile is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

















  • 1





    Why are you using $@ at all?

    – Wildcard
    2 days ago











  • @Wildcard Because I had copy/pasted that line from someone else who was apparently also a beginner like me. :S

    – Théophile
    yesterday














0












0








0








I have aliased pushd in my bash shell as follows so that it suppresses output:



alias pushd='pushd "$@" > /dev/null'


This works fine most of the time, but I'm running into trouble now using it inside functions that take arguments. For example,



test() {
pushd .
...
}


Running test without arguments is fine. But with arguments:



> test x y z
bash: pushd: too many arguments


I take it that pushd is trying to take . x y z as arguments instead of just .. How can I prevent this? Is there a "local" equivalent of $@ that would only see . and not x y z?










share|improve this question









New contributor



Théophile is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I have aliased pushd in my bash shell as follows so that it suppresses output:



alias pushd='pushd "$@" > /dev/null'


This works fine most of the time, but I'm running into trouble now using it inside functions that take arguments. For example,



test() {
pushd .
...
}


Running test without arguments is fine. But with arguments:



> test x y z
bash: pushd: too many arguments


I take it that pushd is trying to take . x y z as arguments instead of just .. How can I prevent this? Is there a "local" equivalent of $@ that would only see . and not x y z?







bash alias function pushd






share|improve this question









New contributor



Théophile is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.










share|improve this question









New contributor



Théophile is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








share|improve this question




share|improve this question








edited 2 days ago









Jeff Schaller

49k11 gold badges72 silver badges162 bronze badges




49k11 gold badges72 silver badges162 bronze badges






New contributor



Théophile is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








asked 2 days ago









ThéophileThéophile

1033 bronze badges




1033 bronze badges




New contributor



Théophile is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




New contributor




Théophile is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.













  • 1





    Why are you using $@ at all?

    – Wildcard
    2 days ago











  • @Wildcard Because I had copy/pasted that line from someone else who was apparently also a beginner like me. :S

    – Théophile
    yesterday














  • 1





    Why are you using $@ at all?

    – Wildcard
    2 days ago











  • @Wildcard Because I had copy/pasted that line from someone else who was apparently also a beginner like me. :S

    – Théophile
    yesterday








1




1





Why are you using $@ at all?

– Wildcard
2 days ago





Why are you using $@ at all?

– Wildcard
2 days ago













@Wildcard Because I had copy/pasted that line from someone else who was apparently also a beginner like me. :S

– Théophile
yesterday





@Wildcard Because I had copy/pasted that line from someone else who was apparently also a beginner like me. :S

– Théophile
yesterday










1 Answer
1






active

oldest

votes


















5














Aliases define a way to replace a shell token with some string before the shell event tries to parse code. It's not a programming structure like a function.



In



alias pushd='pushd "$@" > /dev/null'


and then:



pushd .


What's going on is that the pushd is replaced with pushd "$@" > /dev/null and then the result parsed. So the shell ends up parsing:



pushd "$@" > /dev/null .


Redirections can appear anywhere on the command line, so it's exactly the same as:



pushd "$@" . > /dev/null


or



> /dev/null pushd "$@" .


When you're running that from the prompt, "$@" is the list of arguments your shell received so unless you ran set arg1 arg2, that will likely be empty, so it will be the same as



pushd . > /dev/null


But within a function, that "$@" will be the arguments of the function.



Here, you either want to define pushd as a function like:



pushd() { command pushd "$@" > /dev/null; }


Or an alias like:



alias pushd='> /dev/null pushd'


or



alias pushd='pushd > /dev/null





share|improve this answer


























  • Amazing! Thank you.

    – Théophile
    2 days ago











  • I had actually thought of writing it as a function, but I didn't know about command yet, so of course it created an infinite loop.

    – Théophile
    2 days ago
















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
});


}
});






Théophile is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f535782%2fin-alias-inside-script-is-there-a-local%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









5














Aliases define a way to replace a shell token with some string before the shell event tries to parse code. It's not a programming structure like a function.



In



alias pushd='pushd "$@" > /dev/null'


and then:



pushd .


What's going on is that the pushd is replaced with pushd "$@" > /dev/null and then the result parsed. So the shell ends up parsing:



pushd "$@" > /dev/null .


Redirections can appear anywhere on the command line, so it's exactly the same as:



pushd "$@" . > /dev/null


or



> /dev/null pushd "$@" .


When you're running that from the prompt, "$@" is the list of arguments your shell received so unless you ran set arg1 arg2, that will likely be empty, so it will be the same as



pushd . > /dev/null


But within a function, that "$@" will be the arguments of the function.



Here, you either want to define pushd as a function like:



pushd() { command pushd "$@" > /dev/null; }


Or an alias like:



alias pushd='> /dev/null pushd'


or



alias pushd='pushd > /dev/null





share|improve this answer


























  • Amazing! Thank you.

    – Théophile
    2 days ago











  • I had actually thought of writing it as a function, but I didn't know about command yet, so of course it created an infinite loop.

    – Théophile
    2 days ago


















5














Aliases define a way to replace a shell token with some string before the shell event tries to parse code. It's not a programming structure like a function.



In



alias pushd='pushd "$@" > /dev/null'


and then:



pushd .


What's going on is that the pushd is replaced with pushd "$@" > /dev/null and then the result parsed. So the shell ends up parsing:



pushd "$@" > /dev/null .


Redirections can appear anywhere on the command line, so it's exactly the same as:



pushd "$@" . > /dev/null


or



> /dev/null pushd "$@" .


When you're running that from the prompt, "$@" is the list of arguments your shell received so unless you ran set arg1 arg2, that will likely be empty, so it will be the same as



pushd . > /dev/null


But within a function, that "$@" will be the arguments of the function.



Here, you either want to define pushd as a function like:



pushd() { command pushd "$@" > /dev/null; }


Or an alias like:



alias pushd='> /dev/null pushd'


or



alias pushd='pushd > /dev/null





share|improve this answer


























  • Amazing! Thank you.

    – Théophile
    2 days ago











  • I had actually thought of writing it as a function, but I didn't know about command yet, so of course it created an infinite loop.

    – Théophile
    2 days ago
















5












5








5







Aliases define a way to replace a shell token with some string before the shell event tries to parse code. It's not a programming structure like a function.



In



alias pushd='pushd "$@" > /dev/null'


and then:



pushd .


What's going on is that the pushd is replaced with pushd "$@" > /dev/null and then the result parsed. So the shell ends up parsing:



pushd "$@" > /dev/null .


Redirections can appear anywhere on the command line, so it's exactly the same as:



pushd "$@" . > /dev/null


or



> /dev/null pushd "$@" .


When you're running that from the prompt, "$@" is the list of arguments your shell received so unless you ran set arg1 arg2, that will likely be empty, so it will be the same as



pushd . > /dev/null


But within a function, that "$@" will be the arguments of the function.



Here, you either want to define pushd as a function like:



pushd() { command pushd "$@" > /dev/null; }


Or an alias like:



alias pushd='> /dev/null pushd'


or



alias pushd='pushd > /dev/null





share|improve this answer













Aliases define a way to replace a shell token with some string before the shell event tries to parse code. It's not a programming structure like a function.



In



alias pushd='pushd "$@" > /dev/null'


and then:



pushd .


What's going on is that the pushd is replaced with pushd "$@" > /dev/null and then the result parsed. So the shell ends up parsing:



pushd "$@" > /dev/null .


Redirections can appear anywhere on the command line, so it's exactly the same as:



pushd "$@" . > /dev/null


or



> /dev/null pushd "$@" .


When you're running that from the prompt, "$@" is the list of arguments your shell received so unless you ran set arg1 arg2, that will likely be empty, so it will be the same as



pushd . > /dev/null


But within a function, that "$@" will be the arguments of the function.



Here, you either want to define pushd as a function like:



pushd() { command pushd "$@" > /dev/null; }


Or an alias like:



alias pushd='> /dev/null pushd'


or



alias pushd='pushd > /dev/null






share|improve this answer












share|improve this answer



share|improve this answer










answered 2 days ago









Stéphane ChazelasStéphane Chazelas

330k58 gold badges642 silver badges1009 bronze badges




330k58 gold badges642 silver badges1009 bronze badges
















  • Amazing! Thank you.

    – Théophile
    2 days ago











  • I had actually thought of writing it as a function, but I didn't know about command yet, so of course it created an infinite loop.

    – Théophile
    2 days ago





















  • Amazing! Thank you.

    – Théophile
    2 days ago











  • I had actually thought of writing it as a function, but I didn't know about command yet, so of course it created an infinite loop.

    – Théophile
    2 days ago



















Amazing! Thank you.

– Théophile
2 days ago





Amazing! Thank you.

– Théophile
2 days ago













I had actually thought of writing it as a function, but I didn't know about command yet, so of course it created an infinite loop.

– Théophile
2 days ago







I had actually thought of writing it as a function, but I didn't know about command yet, so of course it created an infinite loop.

– Théophile
2 days ago












Théophile is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















Théophile is a new contributor. Be nice, and check out our Code of Conduct.













Théophile is a new contributor. Be nice, and check out our Code of Conduct.












Théophile is a new contributor. Be nice, and check out our Code of Conduct.
















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f535782%2fin-alias-inside-script-is-there-a-local%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

Nicolae Petrescu-Găină Cuprins Biografie | Opera | In memoriam | Varia | Controverse, incertitudini...