You can set the internal field separator (IFS) variable, and then let it parse into an array. When this happens in a command, then the assignment to IFS
only takes place to that single command’s environment (to read
). It then parses the input according to the IFS
variable value into an array, which we can then iterate over.
This example will parse one line of items separated by ;
, pushing it into an array:
IFS=';' read -ra ADDR <<< "$IN" for i in "${ADDR[@]}"; do # process "$i" done
This other example is for processing the whole content of $IN
, each time one line of input separated by ;
:
while IFS=';' read -ra ADDR; do for i in "${ADDR[@]}"; do # process "$i" done done <<< "$IN"
Related Posts:
- How do I split a string on a delimiter in Bash?
- Ubuntu says “bash: ./program Permission denied” [closed]
- How to concatenate string variables in Bash
- -bash: syntax error near unexpected token `)’
- An example of how to use getopts in bash
- How to mkdir only if a directory does not already exist?
- How do I iterate over a range of numbers defined by variables in Bash?
- How do I iterate over a range of numbers defined by variables in Bash?
- How to mkdir only if a directory does not already exist?
- “unary operator expected” error in Bash if condition
- What does “-ne” mean in bash?
- How can I declare and use Boolean variables in a shell script?
- How can I declare and use Boolean variables in a shell script?
- How to generate random number in Bash?
- How to check if a variable is set in Bash?
- “[ ]” vs. “[[ ]]” in Bash shell
- How can I compare numbers in Bash?
- Open and write data to text file using Bash?
- How do I set a variable to the output of a command in Bash?
- What is the $? (dollar question mark) variable in shell scripting?
- Is there a TRY CATCH command in Bash
- Run a string as a command within a Bash script
- How to split one string into multiple strings separated by at least one space in bash shell?
- Shell script not running, command not found
- How do I know the script file name in a Bash script?
- What is the difference between double and single square brackets in bash?
- What is “-bash: !”: event not found”
- “No such file or directory” but it exists
- How do I pause my shell script for a second before continuing?
- How to split a string into an array in Bash?
- In the shell, what does ” 2>&1 ” mean?
- Extract substring in Bash
- Should I put #! (shebang) in Python scripts, and what form should it take?
- Should I put #! (shebang) in Python scripts, and what form should it take?
- In the shell, what does ” 2>&1 ” mean?
- Difference between wait and sleep
- How do I parse command line arguments in Bash?
- Multi-line string with extra space (preserved indentation)
- How to delete from a text file, all lines that contain a specific string?
- Multi-line string with extra space (preserved indentation)
- ‘\r’: command not found – .bashrc / .bash_profile [duplicate]
- ‘\r’: command not found – .bashrc / .bash_profile [duplicate]
- How can I check if a directory exists in a Bash shell script?
- How to set ssh timeout?
- How to split a string into an array in Bash?
- sudo: apt-get: command not found
- Why do people write #!/usr/bin/env python on the first line of a Python script?
- Using find to locate files that match one of multiple patterns
- “sed” command in bash
- Is there a Python equivalent to the ‘which’ command
- How to reload .bash_profile from the command line?
- What is the purpose of “&&” in a shell command?
- How does “cat << EOF" work in bash?
- Bash scripting missing ‘]’
- How do I compare two string variables in an ‘if’ statement in Bash?
- Who can access a file with octal permissions “000” on Linux/UNIX?
- What is a list in Bash?
- What does the line “#!/bin/sh” mean in a UNIX shell script?
- How can I count all the lines of code in a directory recursively?
- How can I kill a process by name instead of PID, on Linux?
- Given two directory trees, how can I find out which files differ by content?
- Which characters need to be escaped when using Bash?
- How to remove the quotes when reading a variable in jq in shell?
- Shell: How to call one shell script from another shell script?
- Pseudo-terminal will not be allocated because stdin is not a terminal
- python getoutput() equivalent in subprocess
- Meaning of $? (dollar question mark) in shell scripts
- ./configure : /bin/sh^M : bad interpreter
- What is the difference between “#!/usr/bin/env bash” and “#!/usr/bin/bash”?
- How to convert a string to lower case in Bash?
- Is there a “goto” statement in bash?
- dquote> result of a execution a program in linux shell
- Using sudo with Python script
- Bash script: bad interpreter
- How to count lines in a document?
- What does `set -x` do?
- conditional binary operator expected in shell script
- How to specify the private SSH-key to use when executing shell command on Git?
- Block Comments in a Shell Script
- Sorting data based on second column of a file
- Writing a simple shell in C using fork/execvp
- Speed up rsync with Simultaneous/Concurrent File Transfers?
- What’s the meaning of the parameter -e for bash shell command line?
- zip error – Nothing to do
- How to sort an array in Bash
- What does set -e mean in a bash script?
- What does set -e mean in a bash script?
- Check existence of input argument in a Bash shell script
- Copy multiple files from one directory to another from Linux shell
- source command not found in sh shell
- How to get a shell environment variable in a makefile?
- using rot13 and tr command for having an encrypted email address
- Shell Script: Execute a python program from within a shell script
- sed whole word search and replace
- Integer expression expected error in shell script
- How to add a timestamp to bash script log?
- What’s the difference betwen the single dash and double dash flags on shell commands?
- How to run command as user who has /usr/sbin/nologin as Shell?
- How to let ‘cp’ command don’t fire an error when source file does not exist?
- How can I fully log all bash scripts actions?