Question about order of variable and pathname expansion in Bash












0














Bash expands variables before expanding pathnames (globbing). Assuming that it did it the other way round (i.e. expand pathnames before expanding variables), is it possible to write a bash script that uses both variable expansion and path expansion, such that the result is the same irrespective of the order of expansion.



I have come up with the following simple bash script.



#!/bin/bash

# Create a variable called myvar1
myvar1=~
echo $myvar1

# Use both path expansion and variable expansion with the ls command
# Assuming the variable was expanded first, the out should be the same as if the path was expanded first. Is my assumption correct?
ls $myvar1/*.txt


Can someone verify that my script answers the question satisfactorily?










share|improve this question




















  • 1




    Not quite: consider a directory containing a file called $myvar1, and you do ls *. This will expand to potentially many files, one of which is $myvar1. Then (in this hypothetical world) this variable will be expanded to ~ and you will see the contents of your home directory (assuming ~ expansion still occurs after that, otherwise it will attempt to show a file literally called ~)
    – Fox
    Mar 23 '17 at 23:07
















0














Bash expands variables before expanding pathnames (globbing). Assuming that it did it the other way round (i.e. expand pathnames before expanding variables), is it possible to write a bash script that uses both variable expansion and path expansion, such that the result is the same irrespective of the order of expansion.



I have come up with the following simple bash script.



#!/bin/bash

# Create a variable called myvar1
myvar1=~
echo $myvar1

# Use both path expansion and variable expansion with the ls command
# Assuming the variable was expanded first, the out should be the same as if the path was expanded first. Is my assumption correct?
ls $myvar1/*.txt


Can someone verify that my script answers the question satisfactorily?










share|improve this question




















  • 1




    Not quite: consider a directory containing a file called $myvar1, and you do ls *. This will expand to potentially many files, one of which is $myvar1. Then (in this hypothetical world) this variable will be expanded to ~ and you will see the contents of your home directory (assuming ~ expansion still occurs after that, otherwise it will attempt to show a file literally called ~)
    – Fox
    Mar 23 '17 at 23:07














0












0








0







Bash expands variables before expanding pathnames (globbing). Assuming that it did it the other way round (i.e. expand pathnames before expanding variables), is it possible to write a bash script that uses both variable expansion and path expansion, such that the result is the same irrespective of the order of expansion.



I have come up with the following simple bash script.



#!/bin/bash

# Create a variable called myvar1
myvar1=~
echo $myvar1

# Use both path expansion and variable expansion with the ls command
# Assuming the variable was expanded first, the out should be the same as if the path was expanded first. Is my assumption correct?
ls $myvar1/*.txt


Can someone verify that my script answers the question satisfactorily?










share|improve this question















Bash expands variables before expanding pathnames (globbing). Assuming that it did it the other way round (i.e. expand pathnames before expanding variables), is it possible to write a bash script that uses both variable expansion and path expansion, such that the result is the same irrespective of the order of expansion.



I have come up with the following simple bash script.



#!/bin/bash

# Create a variable called myvar1
myvar1=~
echo $myvar1

# Use both path expansion and variable expansion with the ls command
# Assuming the variable was expanded first, the out should be the same as if the path was expanded first. Is my assumption correct?
ls $myvar1/*.txt


Can someone verify that my script answers the question satisfactorily?







bash scripting variable






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday









Rui F Ribeiro

38.8k1479128




38.8k1479128










asked Mar 23 '17 at 23:00









Automation Zombie

172




172








  • 1




    Not quite: consider a directory containing a file called $myvar1, and you do ls *. This will expand to potentially many files, one of which is $myvar1. Then (in this hypothetical world) this variable will be expanded to ~ and you will see the contents of your home directory (assuming ~ expansion still occurs after that, otherwise it will attempt to show a file literally called ~)
    – Fox
    Mar 23 '17 at 23:07














  • 1




    Not quite: consider a directory containing a file called $myvar1, and you do ls *. This will expand to potentially many files, one of which is $myvar1. Then (in this hypothetical world) this variable will be expanded to ~ and you will see the contents of your home directory (assuming ~ expansion still occurs after that, otherwise it will attempt to show a file literally called ~)
    – Fox
    Mar 23 '17 at 23:07








1




1




Not quite: consider a directory containing a file called $myvar1, and you do ls *. This will expand to potentially many files, one of which is $myvar1. Then (in this hypothetical world) this variable will be expanded to ~ and you will see the contents of your home directory (assuming ~ expansion still occurs after that, otherwise it will attempt to show a file literally called ~)
– Fox
Mar 23 '17 at 23:07




Not quite: consider a directory containing a file called $myvar1, and you do ls *. This will expand to potentially many files, one of which is $myvar1. Then (in this hypothetical world) this variable will be expanded to ~ and you will see the contents of your home directory (assuming ~ expansion still occurs after that, otherwise it will attempt to show a file literally called ~)
– Fox
Mar 23 '17 at 23:07










1 Answer
1






active

oldest

votes


















0














ls $myvar1/*.txt 


If pathnames were expanded before variables, this would look for filenames matching *.txt in a directory called $myvar1, which you probably wouldn't have. After that, it would expand $myvar1 to the path to your home directory, leaving /home/username/*.txt, which is not the same as expanding the path first and then looking for filenames ending in .txt in the home directory.



In general, since file names can contain $ signs, I think the order of expansion will always matter. Consider any script using a glob like * in a directory containing a file called $PATH.



The opposite, variables containing glob characters, is easier to work around, since you control the values of your variables.






share|improve this answer





















  • Makes sense. Thanks!
    – Automation Zombie
    Mar 27 '17 at 16:55











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


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f353447%2fquestion-about-order-of-variable-and-pathname-expansion-in-bash%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









0














ls $myvar1/*.txt 


If pathnames were expanded before variables, this would look for filenames matching *.txt in a directory called $myvar1, which you probably wouldn't have. After that, it would expand $myvar1 to the path to your home directory, leaving /home/username/*.txt, which is not the same as expanding the path first and then looking for filenames ending in .txt in the home directory.



In general, since file names can contain $ signs, I think the order of expansion will always matter. Consider any script using a glob like * in a directory containing a file called $PATH.



The opposite, variables containing glob characters, is easier to work around, since you control the values of your variables.






share|improve this answer





















  • Makes sense. Thanks!
    – Automation Zombie
    Mar 27 '17 at 16:55
















0














ls $myvar1/*.txt 


If pathnames were expanded before variables, this would look for filenames matching *.txt in a directory called $myvar1, which you probably wouldn't have. After that, it would expand $myvar1 to the path to your home directory, leaving /home/username/*.txt, which is not the same as expanding the path first and then looking for filenames ending in .txt in the home directory.



In general, since file names can contain $ signs, I think the order of expansion will always matter. Consider any script using a glob like * in a directory containing a file called $PATH.



The opposite, variables containing glob characters, is easier to work around, since you control the values of your variables.






share|improve this answer





















  • Makes sense. Thanks!
    – Automation Zombie
    Mar 27 '17 at 16:55














0












0








0






ls $myvar1/*.txt 


If pathnames were expanded before variables, this would look for filenames matching *.txt in a directory called $myvar1, which you probably wouldn't have. After that, it would expand $myvar1 to the path to your home directory, leaving /home/username/*.txt, which is not the same as expanding the path first and then looking for filenames ending in .txt in the home directory.



In general, since file names can contain $ signs, I think the order of expansion will always matter. Consider any script using a glob like * in a directory containing a file called $PATH.



The opposite, variables containing glob characters, is easier to work around, since you control the values of your variables.






share|improve this answer












ls $myvar1/*.txt 


If pathnames were expanded before variables, this would look for filenames matching *.txt in a directory called $myvar1, which you probably wouldn't have. After that, it would expand $myvar1 to the path to your home directory, leaving /home/username/*.txt, which is not the same as expanding the path first and then looking for filenames ending in .txt in the home directory.



In general, since file names can contain $ signs, I think the order of expansion will always matter. Consider any script using a glob like * in a directory containing a file called $PATH.



The opposite, variables containing glob characters, is easier to work around, since you control the values of your variables.







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 24 '17 at 10:37









ilkkachu

55.3k782150




55.3k782150












  • Makes sense. Thanks!
    – Automation Zombie
    Mar 27 '17 at 16:55


















  • Makes sense. Thanks!
    – Automation Zombie
    Mar 27 '17 at 16:55
















Makes sense. Thanks!
– Automation Zombie
Mar 27 '17 at 16:55




Makes sense. Thanks!
– Automation Zombie
Mar 27 '17 at 16:55


















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f353447%2fquestion-about-order-of-variable-and-pathname-expansion-in-bash%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

サソリ

広島県道265号伴広島線

Accessing regular linux commands in Huawei's Dopra Linux