shopt

Make shopt change local to function

人走茶凉 提交于 2020-01-29 05:49:04
问题 I'm trying to write a bash function that uses nocasematch without changing the callers setting of the option. The function definition is: is_hello_world() { shopt -s nocasematch [[ "$1" =~ "hello world" ]] } Before I call it: $ shopt nocasematch nocasematch off Call it: $ is_hello_world 'hello world' && echo Yes Yes $ is_hello_world 'Hello World' && echo Yes Yes As expected, but now nocasematch of the caller has changed: $ shopt nocasematch nocasematch on Is there any easy way to make the

How to match nothing if a file name glob has no matches [duplicate]

送分小仙女□ 提交于 2019-12-21 07:25:30
问题 This question already has answers here : How to skip the for loop when there are no matching files? (2 answers) Closed 6 months ago . I want to loop over all files matching extension jpg or txt . I use: for file in myDir/*.{jpg,txt} do echo "$file" done Problem: If the directory contains no jpg file at all, the loop will have one iteration with output myDir/*.jpg . I thought * will be replaced by an arbitrary file (and if no file exists it cannot be expanded). How can I avoid the unwanted

Is there an easy way to set nullglob for one glob

烂漫一生 提交于 2019-12-20 12:07:08
问题 In bash, if you do this: mkdir /tmp/empty array=(/tmp/empty/*) you find that array now has one element, "/tmp/empty/*" , not zero as you'd like. Thankfully, this can be avoided by turning on the nullglob shell option using shopt -s nullglob But nullglob is global, and when editing an existing shell script, may break things (e.g., did someone check the exit code of ls foo* to check if there are files named starting with "foo"?). So, ideally, I'd like to turn it on only for a small scope

What do double-asterisk wildcards mean?

允我心安 提交于 2019-12-17 15:36:50
问题 I've tried the following command but I am having trouble interpreting it: ls ** but I'm not sure exactly what it is outputting and why that is the result. 回答1: Keith's answer is the correct one. Refer there. Consider the following directory tree: folderA ├── file1 ├── file2 └── folderB ├── file3 └── folderC ls will list all objects in the current folder: $ ls file1 file2 folderB ls * will list all objects in the current folder and in addition one more level recursively $ ls * file1 file2

What expands to all files in current directory recursively?

北城以北 提交于 2019-12-17 04:52:12
问题 I know **/*.ext expands to all files in all subdirectories matching *.ext , but what is a similar expansion that includes all such files in the current directory as well? 回答1: This will work in Bash 4: ls -l {,**/}*.ext In order for the double-asterisk glob to work, the globstar option needs to be set (default: on): shopt -s globstar From man bash : globstar If set, the pattern ** used in a filename expansion con‐ text will match a files and zero or more directories and subdirectories. If the

How to skip the for loop when there are no matching files?

眉间皱痕 提交于 2019-12-17 03:21:29
问题 When I loop through all the files starting by foo I do for f in foo* ; do echo "result = $f" ; done The problem is when no file start by foo I get: result = foo* Meaning that the loop is executed once, even if no file start by foo . How is this possible? How can I loop through all files (and not loop at all if there is no file)? 回答1: You can stop this behaviour by setting nullglob: shopt -s nullglob From the linked page: nullglob is a Bash shell option which modifies [[glob]] expansion such

How to skip the for loop when there are no matching files?

雨燕双飞 提交于 2019-12-17 03:20:01
问题 When I loop through all the files starting by foo I do for f in foo* ; do echo "result = $f" ; done The problem is when no file start by foo I get: result = foo* Meaning that the loop is executed once, even if no file start by foo . How is this possible? How can I loop through all files (and not loop at all if there is no file)? 回答1: You can stop this behaviour by setting nullglob: shopt -s nullglob From the linked page: nullglob is a Bash shell option which modifies [[glob]] expansion such

nullglob disables pathname tab-completion

杀马特。学长 韩版系。学妹 提交于 2019-12-10 09:23:29
问题 I have found that shopt -s nullglob apparently disables tab-completion for files and directories, and shopt -u nullglob restores it. Why does tab-completion for directories apparently rely on nullglob being unset? I am using Bash 4.2.37(1)-release on Debian 7. 回答1: This is apparently a known issue with bash-completion and is listed as an objective to be fixed in the 3.0 version. But apparently it has been that way since at least 2012. See https://bugs.debian.org/cgi-bin/bugreport.cgi?bug

nullglob and arrays

不想你离开。 提交于 2019-12-08 02:01:50
问题 I can create an array, then delete from this array $ foo=(a b c) $ unset foo[0] $ echo ${foo[*]} b c However if nullglob is set then I cannot delete from the array $ shopt -s nullglob $ foo=(a b c) $ unset foo[0] $ echo ${foo[*]} a b c 回答1: unset 'foo[0]' Bash thinks the var[1] is a glob, doesn't find a file that matches it, and per instruction of nullglob removes it, causing your script to run unset instead of unset var[1] - and nothing gets unset. The correct way to fix this issue is to

How to match nothing if a file name glob has no matches [duplicate]

痴心易碎 提交于 2019-12-04 01:05:09
This question already has answers here : Closed 5 months ago . How to skip the for loop when there are no matching files? (2 answers) I want to loop over all files matching extension jpg or txt . I use: for file in myDir/*.{jpg,txt} do echo "$file" done Problem: If the directory contains no jpg file at all, the loop will have one iteration with output myDir/*.jpg . I thought * will be replaced by an arbitrary file (and if no file exists it cannot be expanded). How can I avoid the unwanted iteration? Use this to avoid the unwanted iteration: shopt -s nullglob From man bash : nullglob : If set,