Skip to content
Bash

Bash の変数と配列

Bash で変数の代入、コマンド置換、配列の操作。

#variable#array#beginner

Code

bash
#!/bin/bash

# String variables
name="Alice"
greeting="Hello, $name"
echo "$greeting"           # Hello, Alice

# Command substitution
current_date=$(date +%Y-%m-%d)
files_count=$(ls | wc -l)

# Default values
echo "${USER:-guest}"      # use 'guest' if USER unset

# Arrays
fruits=("apple" "banana" "cherry")
echo "${fruits[0]}"        # apple
echo "${fruits[@]}"        # all
echo "${#fruits[@]}"       # length: 3

# Read user input
read -p "Enter your name: " username
echo "Hi, $username"