Writing a bash command, path in variable, how to use expansion?
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Writing a bash command, path in variable, how to use expansion?
Hello, people. I am trying to write a command line in Bash using a shell variable containing a path to some directory.
I want to use shell expansion in the argument where the variable is used. This could cause the variable to be substituted by its value, but I prefer it does not.
I am using Bash and Debian here. What is happening now, in the below pseudo meta command lines are the following moments:
Code:
# The variable's name is DIR
tar xf $DIR<moment 1>
# After typing the variable name, I press TAB wanting to see the
# directory's contents (only part of them will be shown¹). But it
# shows 2 variables, after pressing TAB twice:
$DIR $DIRSTACK
# Fine. I type a "/" in the command line, "to make clear" which
# variable I am using, and type TAB after it.
# In this moment, problem 1 happens. The money symbol is ESCAPED
# and the command line becomes:
tar xf \$DIR/
# With the line above, everything I want is useless. So, I remove
# the "\" from it. If I press TAB right after the "/", it will
# quote the "$", nothing else. But if I type part of the name of
# a few files or directories in the $DIR folder, I can press TAB
# one time and have this name expanded. For
# example, suppose there are several files and folders with the
# name "vivaldi" followed by a date in international format, like
# "YYYY,MM,DD,HH,mm,SS", followed by possibly different things.
# I type "viv" and press TAB, and problem 2 happens because the
# command becomes:
tar xf \$DIR/vivaldi
# Now, the command line expansion is basically useless! If I do not
# know enough about a file name in the $DIR folder, I cannot use
# it anymore. I can, but I have to list the directory (in another
# terminal?) and see the folder's contents separately.
# ¹ This is something that did not happen before in Bash. It
# seems to be something that has been more recently added. It
# is nice, sometimes, that a command argument only considers
# files with certain things in its name. For example, a tar
# command ignores files ending with ".png" or files without
# extension. BUT this is annoying when I want the name of
# that file in the tar argument! And what I do, to make this
# happen, is typing any trash as the first word in the
# command line, pressing TAB to everything I want in the
# tar command, deleting the trash word, and only then,
# executing the command. I want to turn this off, or know
# how to program it better (with my preference for this
# situation, for example). How/what/where?
The thread above does not solve the problem. There are things there that did not work. And I think the question I made in the last part of the thread is not answered. The superuser page information, pointed there, is useless, since it is all mentioned in the above thread.
they discuss exactly the same thing, as far as I see.
They discuss the same, true. But there is no solution (clear?) there, that thread is not solved. So, removing the details that I carefully putted in the first post, I want to:
1. Use a Bash shell variable with a folder path, as part of an argument in a command line. This is simple only if I know the contents of the "magic" folder the variable points to.
2. Use expansion of files or directories names to compose a command using this variable. (detailed problem above)
3. Expand the variable, before finishing the command that will be executed. But I prefer the variable being kept in the command line. (asked above)
4. Escaping the "$" of a variable name after partially expanding it with its value is totally stupid! If the variable is used in an expansion, it does not make sense to escape the "$" in its name. For me, this is a bug - but maybe people here can show me something in my setup that is causing the problem. This is what happens, and it is something mentioned in that other thread, but no solution to either choice is shown - or I do not understand. (I simply cannot break either of these 2 mutually excluding behaviours)
Yes, you are right, that is probably still unanswered. I just wanted to suggest you to join to that discussion, because it is the same topic. I have to say I'm a bit clueless, I don't know what would be the ideal solution, and if it exists at all.
I appreciate your efforts in providing this fantastic information. I'm delighted to have read this content. Please continue sharing similar content in the future. Here you can also visit this link (spam link snipped)check it for amazing experience.
Yes, you are right, that is probably still unanswered. I just wanted to suggest you to join to that discussion, because it is the same topic. I have to say I'm a bit clueless, I don't know what would be the ideal solution, and if it exists at all.
I do not want to simply join that thread because: my situation may not be exactly the same situation which was started there; my knowledge (or lack of) is different from the other person; everything I understood from there, is said here too (this part seems to be what disturbs you); when I am able to solve the problem, possibly learning something new, I will mark this thread as solved, and (maybe) the other person will too, and the other thread will not be abandoned as a possibly eternal unsolved question.
Escaping the $DIR to \$DIR is a bug in your bash version.
My older bash doesn't do this.
Still my bash needs one <tab> too many to offer $DIR $DIRSTACK
I can live with it.
The <tab> completion is complicated, and bugs are frequently popping up, especially in bash.
Run
zsh
and experience how <tab> completion really works!
In zsh run
emulate bash
to make it similar to bash.
To return to bash run
exit
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.