본문으로 바로가기

리눅스 echo 명령어 사용법 ( -n, -e , -E , --help)

category 리눅스 (linux)/명령어 2022. 2. 17. 13:41

: 텍스트 한줄 표시 명령어

Manual page

 

0> 도표

옵션
의미
기타
-n
마지막 개행을 않하는 옵션
목차 참조
-e
백슬러시 제어문자 사용옵션
목차 참조
-E
백슬러시 제어문자 미사용 옵션
목차참조
--help
도움말 (/bin/echo --help)
목차 참조

 

목차

     

    < 기본예제>

     :  echo + 문자열

      $ echo "hello" or $ echo hello

        hello

    >> echo + 문자(열)*

     

    $ echo "hello"
    hello
    
    $ echo hello
    hello
    
    $ ls
    0001-linux-ed-test.patch  d.txt      ls.txt           reset_mixed.txt
    0002-b.txt.patch          e.txt      old              reset_soft.txt
    a.txt                     f.txt      old2             tee_test.txt
    b.txt                     ls_A1.txt  reset_hard.txt   wc_test.txt
    content                   ls_b.txt   reset_keep.txt
    d_e.txt                   ls_l.txt   reset_merge.txt
    
    $ echo ls*
    ls_A1.txt ls_b.txt ls_l.txt ls.txt
     >> ls 로 시작하는 파일 출력

     

    >> $ 사용한 변수 내용 출력하기 

    $ export TEST="hello"
    
    >> TEST 변수값 출력
    $ echo $TEST
    hello
    
    >> TEST 변수 저장 확인은 set && grep 사용 
    $ set | grep TEST
    TEST=hello

     

     

    옵션

    -n 옵션

    : 후행 줄바꿈 제거 옵션

    /c/Project/gitTest/treeTest$echo "hello 123"
    hello 123
    
    /c/Project/gitTest/treeTest$echo -n "hello 123"
    hello 123/c/Project/gitTest/treeTest$

     

     -e 옵션

    : 백슬래시 이후 제어문자 사용 옵션

    $ echo -e "hello \a"
    hello
    >>> 경고음 출력
    
    $ echo "hello\n 123"
    hello\n 123
    
    $ echo -e "hello\n 123"
    hello
     123

     

      -E 옵션 (default)

    : 백슬래시 이후 제어문자 미인식 옵션

    >> \n 을 개행이 아닌 문자로 인식
    $ echo -E "hello\n"
    hello\n
    
    >> 옵션 미사용시 \n 을 개행이 아닌 문자로 인식
    $ echo "hello\n"
    hello\n
    
    >> \n 을 개행으로 문자로 인식
    $ echo -e "hello\n"

     

    --help 옵션

    : 도움말 표시 ( echo 명령이 아닌 /bin/echo 명령으로 실행)

    >> echo 명령 은 셀빌트인 명령이다.
    $ type echo
    echo is a shell builtin
    
    >> 다음의 --help 는 우리가 원하는 도움말이 나오지 않는다.
    $ echo --help
    --help
    
    >> which 를 이용해 echo 명령 위치를 찾기
    $ which echo
    /bin/echo
    
    $ /bin/echo --help
    Usage: /bin/echo [SHORT-OPTION]... [STRING]...
      or:  /bin/echo LONG-OPTION
    Echo the STRING(s) to standard output.
    
      -n             do not output the trailing newline
      -e             enable interpretation of backslash escapes
      -E             disable interpretation of backslash escapes (default)
          --help     display this help and exit
          --version  output version information and exit
    
    If -e is in effect, the following sequences are recognized:
    
      \\      backslash
      \a      alert (BEL)
      \b      backspace
      \c      produce no further output
      \e      escape
      \f      form feed
      \n      new line
      \r      carriage return
      \t      horizontal tab
      \v      vertical tab
      \0NNN   byte with octal value NNN (1 to 3 digits)
      \xHH    byte with hexadecimal value HH (1 to 2 digits)
    
    NOTE: your shell may have its own version of echo, which usually supersedes
    the version described here.  Please refer to your shell's documentation
    for details about the options it supports.
    
    Report echo bugs to bug-coreutils@gnu.org
    GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
    General help using GNU software: <http://www.gnu.org/gethelp/>
    For complete documentation, run: info coreutils 'echo invocation'

     

    -e 옵션 제어문자 정리

    >> backslash 표시 (-e ,-E 동일한 결과 출력)
    $ echo -e hello\\
    hello\
    
    >> 경고음 출력
    $ echo -e  "\a"
    or 
    $ echo -e  '\a'
    
    >> 백스페이스 사용 (hello ==> hell)
    $ echo -e  "hello\b 1234"
    hell 1234
    
    >> 더이상 출력이 없게 하는 옵션
    $ echo -e  "hello \c"
    hello $
    $ echo -e  "hello "
    hello
    $
    
    >> escape 사용하기
    p$ echo -e  "hello \e  1234"
    hello 234
    
    >> form feed 처리 
      이건 셀에 따라 다르게 나옵니다.
      개행만 되는 경우도 있고,\f 이후 표시 라인이 0,0 으로 이동 하는 경우도 있습니다.
    $ echo -e  "hello \f  1234"
    hello
      1234
    
    >> 개행 (\n)
    $ echo -e  "hello \n 1234"
    hello
     1234
    
    >> \r (carriage return) 
    $ echo -e  "hello \r 12"
     12lo
    
    >> horitontal tab (\t)
    $ echo -e  "hello \t 12"
    hello    12
    
    >> vertical tab (\v)
    $ echo -e  "hello \v 12"
    hello
           12
    
    >> 8진수 값 표시 (\0NNN)
       A 문자 (10진수 65 ,16진수 0x41 , 8진수 101)
    $ echo -e  "hello \0101 12"
    hello A 12
    >> 16진수 값 표시 (\xHH)
    $ echo -e  "hello \x41 12"
    hello A 12

     

    < 기타 >

    ls_l.txt 파일을 echo 를 이용해 찾기

    $ ls ls*
    ls_A1.txt  ls_b.txt  ls_l.txt  ls.txt
    
    $ echo ls????
    ls.txt
    
    $ echo ls???
    ls???
    
    $ echo ls?????
    ls?????

    >> '?' 는 한개의 아무문자 라는 뜻입니다.

    $ echo ls????

    >> ls 라는 문자로 시작하고 그뒤에 4글자가 들어가는 파일/디렉토리 를 찾아주네요.

    만약 없으면 그냥 "ls????" 출력합니다.

     

     

    셀스크립트 사용 예

    echo on     >>  이명령 이후 echo 명령이 화면에 출력됨.
    
    echo off    >> 이명령이후 echo 명령이 화면에 출력안됨.
    
    젤앞에 @를 붙여주면, @를 붙여준 명령어는 화면에 표시 되지 않고,
    명령어를 통해 실행된 내용만 화면에 보여집니다.
    @echo on
    @echo off

     

    마지막 명령 exit status 출력하기

    $ ls
    a.txt  du_1.txt  du_2.txt  du_dir
    
    >> a.txt 파일의 존재 여부 체킹
    $ test -e a.txt
    $ echo $?
    0    >> 파일이 존재함.
    
    >> a123.txt 파일의 존재 여부 체킹
    t$ test -e a123.txt
    $ echo $?
    1    >> 파일이 없음.

     

     

     

     

    감사합니다.

     

    반응형