BASH File Expansion

General support questions
Post Reply
verbal_666
Posts: 8
Joined: 2021/04/10 10:00:36

BASH File Expansion

Post by verbal_666 » 2022/10/25 09:22:52

Hi.
A stupid, maybe :roll: , question:

HOW can i prevent BASH to expand FileSystem integration when using special chars inside a command?

I mean, having the /tmp looks like this,

Code: Select all

/tmp
├── dir
│   └── test2
└── test

Now, i want to simply apply a for cycle with my strings, without having them expanded to FileSystem,

Code: Select all

s="/tmp/* /tmp/dir/*"
for f in $s;do echo "$f";done
OUTPUT:
/tmp/dir
/tmp/test
/tmp/dir/test2


The for command expands my string to a FileSystem expanded object!!!


What i need is a simple,

OUTPUT:
/tmp/*
/tmp/dir/*


The only way i found is to trick the system and add a "pattern" to exlude FileSystem expansion,

Code: Select all

s="^/tmp/* ^/tmp/dir/*"
for f in $s;do echo "${f/^/}";done
And i get my OUTPUT:
/tmp/*
/tmp/dir/*


Is there another way, OS/Bash level, to make this expansion disabled?
Thanks.
:ugeek:

User avatar
TrevorH
Site Admin
Posts: 33218
Joined: 2009/09/24 10:40:56
Location: Brighton, UK

Re: BASH File Expansion

Post by TrevorH » 2022/10/25 11:37:14

Try

s="/tmp/\* /tmp/dir/\*"
The future appears to be RHEL or Debian. I think I'm going Debian.
Info for USB installs on http://wiki.centos.org/HowTos/InstallFromUSBkey
CentOS 5 and 6 are deadest, do not use them.
Use the FAQ Luke

User avatar
jlehtone
Posts: 4530
Joined: 2007/12/11 08:17:33
Location: Finland

Re: BASH File Expansion

Post by jlehtone » 2022/10/25 12:27:18

The '\' appears in output, at least on interactive command.
(One must remember that a typed command and a command in script can expand differently.)

man bash (and more clearly info bash) says in section "Path expansion"
that if '-f' is set, then globbing does not occur. Hence:

Code: Select all

set -f ; for f in $s ; do echo $f ; done ; set +f

owl102
Posts: 413
Joined: 2014/06/10 19:13:41

Re: BASH File Expansion

Post by owl102 » 2022/10/25 22:03:27

An alternative would be using an bash array:

Code: Select all

s=( "/tmp/*" "tmp/dir/*" )
for f in "${s[@]}";do echo "$f";done
German speaking forum for Fedora and CentOS: https://www.fedoraforum.de/

verbal_666
Posts: 8
Joined: 2021/04/10 10:00:36

Re: BASH File Expansion

Post by verbal_666 » 2022/10/27 09:41:21

jlehtone wrote:
2022/10/25 12:27:18
man bash (and more clearly info bash) says in section "Path expansion"
that if '-f' is set, then globbing does not occur. Hence:

Code: Select all

set -f ; for f in $s ; do echo $f ; done ; set +f
GREEEEEEEEEEEEEEEEAT!!! That is what i was looking for!!! I read the man bash, but really didn't find!!! My fault. HERE IT IS!!! 👍👍👍👍👍
Pathname Expansion
After word splitting, unless the -f option has been set, bash scans each word for the characters *, ?, and [. If one of these characters appears, then the word is regarded as a pattern,and replaced with an alphabetically sorted list of file names matching the pattern. If no matching file names are found, and the shell option nullglob is not enabled, the word is left unchanged. If the nullglob option is set, and no matches are found, the word is removed. If the failglob shell option is set, and no matches are found, an error message is printed and the command is not executed. If the shell option nocaseglob is enabled, the match is performed without regard to the case of alphabetic characters. When a pattern is used for pathname expansion, the character ``.'' at the start of a name or immediately following a slash must be matched explicitly, unless the shell option dotglob is set. When matching a pathname, the slash character must always be matched explicitly. In other cases, the ``.'' character is not treated specially. See the description of shopt below under SHELL BUILTIN COMMANDS for a description of the nocaseglob, nullglob, failglob, and dotglob shell options.
Really thanks!!! 🤟🤟🤟🤟🤟

Post Reply