Mailing List Archive

Support open source code!


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [tlug] Bash syntax question



On Wed, Jan 30, 2002 at 03:03:50PM +0900, Frank Bennett wrote:
> I want to pass a set of array values to a function in a Bash script.

I've worked out a solution to this that only makes me feel a little bit sick
when I have to look at it.

The trick seems to be put some character that is not used in the expanded
text at the start of the IFS delimiter variable.  Here's an irritating demo:

#!/bin/bash

function ask_questions () {
  LOCAL_ARRAY=($1)
  shift
  PUBLIC_ARRAY=("${QUESTIONS[@]}")
  echo === Assignment via parameter ===
  for i in "${LOCAL_ARRAY[@]}"; do
    echo "${i}" $1
  done
  echo === Assignment from global variable ===
  for i in "${PUBLIC_ARRAY[@]}"; do
    echo "${i}" $1
  done
}

echo ==================================
echo Trial 1: IFS left at default value
echo ==================================

QUESTIONS=("1. WHY?"  "2. REALLY, WHY?"  "3. WELL?")
ask_questions "${QUESTIONS[*]}" Huh?

echo ======================================
echo Trial 2: IFS set to TAB for assignment
echo ======================================

IFS=$'\t'
QUESTIONS=("1. WHY?"  "2. REALLY, WHY?"  "3. WELL?")
ask_questions "${QUESTIONS[*]}" Huh?


Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links