0

The following outputs the directory display for the tmp directory. Why, and how to stop it.

#!/bin/bash
cd /tmp
echo '*' >zzz
cat zzz
IFS='' read  something <zzz
echo ${something}
Bill Gradwohl
  • 385
  • 4
  • 18
  • 1
    `something=*` `echo ${something}` causes it to be expanded to the contents of the current directory. If you don't want it to expand, enclose the variable expansion in quotation marks e.g. `echo "$something"`. – Anya Shenanigans Jan 01 '22 at 16:14
  • 1
    If you fed this script to [shellcheck](https://www.shellcheck.net), then it would have shown `(info): Double quote to prevent globbing and word splitting.` for the echo line – Anya Shenanigans Jan 01 '22 at 16:17
  • I haven't done any BASH in years and forgot about using double quotes. Thank You. – Bill Gradwohl Jan 01 '22 at 20:26

1 Answers1

0

Quote your expansions.

echo "${something}"

See Parameter Expansion, Word Splitting and Filename Expansion.

The shell scans the results of parameter expansion, command substitution, and arithmetic expansion that did not occur within double quotes for word splitting.

konsolebox
  • 72,135
  • 12
  • 99
  • 105