본문 바로가기

리눅스

[WIP] command 명령어

728x90

command 명령어

coreutils는 리눅스 시스템의 기본 유틸리티 패키지 중 하나입니다. 대부분의 리눅스 배포판에서 이미 설치되어 있지만 설치되어 있지 않은 경우에는 패키지 관리자를 사용하여 설치할 수 있습니다.

coreutils 패키지 설치

Ubuntu 및 Debian 기반 시스템

sudo apt update
sudo apt install -y coreutils

CentOS 및 RHEL 기반 시스템

sudo yum install -y coreutils

Fedora 기반 시스템

sudo dnf install -y coreutils

Arch Linux 기반 시스템

sudo pacman -Syu coreutils

설치 후에는 'command' 명령어를 사용할 수 있습니다.

기본 구문

command [옵션] [명령어] [인수...]

주요 옵션

  • -p: 명령어를 실행할 때 PATH 환경 변수를 무시하고 실행 가능한 파일을 검색합니다.
  • -V 또는 --version: command 명령어의 버전 정보를 표시합니다.
  • -h 또는 --help: command 명령어의 도움말을 표시합니다.

command 명령어의 도움말 표시

더보기
$ command --help
command: command [-pVv] command [arg ...]
    Execute a simple command or display information about commands.

    Runs COMMAND with ARGS suppressing  shell function lookup, or display
    information about the specified COMMANDs.  Can be used to invoke commands
    on disk when a function with the same name exists.

    Options:
      -p    use a default value for PATH that is guaranteed to find all of
            the standard utilities
      -v    print a description of COMMAND similar to the `type' builtin
      -V    print a more verbose description of each COMMAND

    Exit Status:
    Returns exit status of COMMAND, or failure if COMMAND is not found.
728x90

command 명령어를 사용하는 일반적인 방법

1. 다른 명령어 실행

command 명령어를 사용하여 다른 명령어를 실행할 수 있습니다. 다음은 "ls" 명령어를 "command"를 사용하여 실행하는 예시입니다.

command ls
root@8a1457850710:~$ command ls
ADirectory  BDirectory

위의 예시에서 "command" 다음에 실행하려는 명령어인 "ls"를 지정합니다. 이렇게 하면 "ls" 명령어가 실행됩니다.

 

2. 명령어 별칭 무시

command 명령어를 사용하여 명령어 별칭을 무시하고 실제 명령어를 실행할 수 있습니다. 예를 들어, "ls" 명령어가 별칭으로 설정되어 있는 경우 실제 "ls" 명령어를 실행하려면 다음과 같이 사용합니다.

command \ls
root@8a1457850710:~$ command \ls
ADirectory  BDirectory

이렇게 하면 별칭이 무시되고 "/bin/ls" 명령어가 실행됩니다.

 

3. 환경 변수 무시

command 명령어를 사용하여 특정 환경 변수를 무시하고 명령어를 실행할 수 있습니다. 예를 들어, "PATH" 환경 변수를 무시하고 "/usr/bin/python"을 실행하려면 다음과 같이 사용합니다.

command -p /usr/bin/python
root@8a1457850710:~$ command -p /usr/bin/python
bash: /usr/bin/python: No such file or directory
root@8a1457850710:~$ command -p /usr/bin/python3
Python 3.10.6 (main, May 29 2023, 11:10:38) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

이렇게 하면 "PATH" 환경 변수에 의존하지 않고 "/usr/bin/python"이 실행됩니다.

 

4. 실행 가능한 파일 검색

command 명령어를 사용하여 PATH 경로에서 실행 가능한 파일을 검색하고 실행할 수 있습니다. 이는 파일의 전체 경로를 지정하지 않고도 실행 가능한 명령어를 실행하는 데 유용합니다.

command -v python
root@8a1457850710:~$ command -v python
root@8a1457850710:~$ command -v python3
/usr/bin/python3

위 명령어는 python 명령어의 전체 경로를 표시합니다.

스크립트 예제

vim zz.sh
#!/bin/bash

if ! command -v ctop >/dev/null; then
    echo "Installing CTOP"
    echo "CTOP version: $(ctop -v | grep -oP '(?<=version )[\d.]+')"
else
    echo "CTOP already installed"
    echo "CTOP version: $(ctop -v | grep -oP '(?<=version )[\d.]+')"
fi
$ bash zz.sh
CTOP already installed
CTOP version: 0.7.7

 

command 명령어는 주로 특수한 상황에서 다른 명령어를 실행하거나 명령어 별칭과 환경 변수를 무시할 때 사용됩니다. 일반적인 상황에서는 command 명령어를 직접적으로 사용할 필요는 없습니다.

 

728x90