기본 Bash 스크립팅

Bash is a command language interpreter. It is widely available on various operating systems and is a default command interpreter on most GNU/Linux systems. The name is an acronym for the ‘Bourne-Again SHell’.



목차


  • Comments
  • Variables
  • Functions
  • Conditionals
  • Loops
  • Execution

  • 코멘트

    # Single line comment
    : '
    This is a
    multi line
    comment
    '
    

    변수

    #!/usr/bin/env bash
    
    NAME="John"
    echo "Hello $NAME!"
    

    기능

    get_name() {
      echo "John"
    }
    
    echo "You are $(get_name)"
    

    조건부

    if [[ -z "$string" ]]; then
      echo "String is empty"
    elif [[ -n "$string" ]]; then
      echo "String is not empty"
    fi
    

    루프

    for i in /etc/rc.*; do
      echo $i
    done
    
    for ((i = 0 ; i < 100 ; i++)); do
      echo $i
    done
    
    for i in {1..5}; do
        echo "Welcome $i"
    done
    

    실행

    Make your bash script executable by running this command.

    sudo chmod +x script.sh
    
    ./script.sh
    

    좋은 웹페이지 즐겨찾기