Don’t use ls
, it’s not intended for this purpose. Use globbing.
shopt -s nullglob
array=(*)
array2=(file*)
array3=(dir/*)
The nullglob
option causes the array to be empty if there are no matches.
More Related Contents:
- In the shell, what does ” 2>&1 ” mean?
- An efficient way to transpose a file in Bash
- Can I export a variable to the environment from a Bash script without sourcing it?
- Bash tool to get nth line from a file
- ‘\r’: command not found – .bashrc / .bash_profile [duplicate]
- Which characters need to be escaped when using Bash?
- How to run the sftp command with a password from Bash script?
- “~/Desktop/test.txt: No such file or directory”
- How to merge two files line by line in Bash
- How to represent multiple conditions in a shell if statement?
- How to provide password to a command that prompts for one in bash?
- Delete files older than 10 days using shell script in Unix [duplicate]
- “Invalid Arithmetic Operator” when doing floating-point math in bash
- What do double-asterisk (**) wildcards mean?
- How does this bash fork bomb work? [duplicate]
- How to check if running in Cygwin, Mac or Linux?
- Creating a string variable name from the value of another string
- What is a unix command for deleting the first N characters of a line?
- Get the date (a day before current time) in Bash
- Is there a way to make bash job control quiet?
- Hiding secret from command line parameter on Unix
- What does the double-asterisk (**) wildcard mean?
- How to cd into a directory with space in the name?
- Fastest possible grep
- Bash script to receive and repass quoted parameters
- Convert Unix timestamp to a date string
- File content into unix variable with newlines
- Using Bash to display a progress indicator (spinner) [duplicate]
- Extract version number from a string
- Left side of pipe is the subshell?