본문 바로가기

카테고리 없음

[linux] What is '$?' in bash shell

반응형

Unlike functions in “real” programming languages, Bash functions don’t allow you to return a value when called. When a bash function completes, its return value is the status of the last statement executed in the function, 0 for success and non-zero decimal number in the 1 - 255 range for failure.

The return status can be specified by using the return keyword, and it is assigned to the variable $?. The return statement terminates the function. You can think of it as the function’s exit status.

 

[Korean Summary]

Bash 쉘은 기본적으로 call 할 때 값을 return 받는 구조를 허용하지 않는다.

가장 마지막으로 수행된 statement의 return value를 '$?' 에 저장한다.

 

#!/bin/bash

my_function () {
  echo "some result"
  return 55
}

my_function
echo $?

 

 

 

#OUTPUT

some result
55
반응형