LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This 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


Reply
  Search this Thread
Old 01-14-2024, 06:19 AM   #1
dedec0
Senior Member
 
Registered: May 2007
Posts: 1,372

Rep: Reputation: 51
Angry 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?
Thank you.
 
Old 01-14-2024, 06:25 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,901

Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
see here: https://www.linuxquestions.org/quest...le-4175732680/
 
1 members found this post helpful.
Old 01-14-2024, 06:46 AM   #3
dedec0
Senior Member
 
Registered: May 2007
Posts: 1,372

Original Poster
Rep: Reputation: 51
Quote:
Originally Posted by pan64 View Post
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.
 
Old 01-14-2024, 08:40 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,901

Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
they discuss exactly the same thing, as far as I see.
 
Old 01-14-2024, 09:57 AM   #5
dedec0
Senior Member
 
Registered: May 2007
Posts: 1,372

Original Poster
Rep: Reputation: 51
Quote:
Originally Posted by pan64 View Post
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)
 
Old 01-14-2024, 11:15 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,901

Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
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.
 
Old 01-27-2024, 11:05 AM   #7
Wervcx
LQ Newbie
 
Registered: Jan 2024
Posts: 3

Rep: Reputation: 0
I appreciate your efforts in providing this fantastic information. I'm delighted to have read this content.

Last edited by jefro; 01-27-2024 at 05:01 PM.
 
Old 01-27-2024, 12:18 PM   #8
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,732

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Quote:
Originally Posted by Wervcx View Post
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.
Reported
 
Old 01-29-2024, 07:28 AM   #9
dedec0
Senior Member
 
Registered: May 2007
Posts: 1,372

Original Poster
Rep: Reputation: 51
Post

Quote:
Originally Posted by pan64 View Post
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.
 
Old 01-31-2024, 06:01 AM   #10
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Use other variable name than DIR so it doesn't expand to DIRSTACK?
(sorry if I didn't follow well)
 
Old 01-31-2024, 09:54 AM   #11
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,805

Rep: Reputation: 1204Reputation: 1204Reputation: 1204Reputation: 1204Reputation: 1204Reputation: 1204Reputation: 1204Reputation: 1204Reputation: 1204
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
 
  


Reply

Tags
bash, command line expansion, variable



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
MZHOU PCIe SATA Card 2 Port - PCI Express SATA 3.0 GEN3 Expansion Card - 6 Gbp/s SATA Controller Expansion Cards with Low-Profile Bracket (C drmozes Slackware - ARM 4 04-19-2023 07:44 PM
I want to know about expansion of variable and path name and how to demonstrate which one occurs first. darkshadow56 Linux - Newbie 4 04-07-2020 04:23 AM
Run command via path expansion doesn't work even when variable is correct grob115 Linux - Server 7 11-09-2015 10:56 PM
variable expansion and pathname expansion ShadeLover Linux - General 6 04-22-2015 10:56 PM
bash script path issue - how to pass a path as a string to a variable PiNPOiNT Programming 5 04-17-2009 05:48 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 01:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration