본문 바로가기

운영체제/리눅스(Linux)

리눅스 소소한 질문 모음

1) Source로 쉘 스크립트 실행 시 권한이 없는 문제

문제 상황)

- source 로 쉘을 실행하려 하니, 관리자 권한이 없다고 하고

- sudo source로 쉘을 실행 하려하니 source 명령을 찾지 못하는 아래와 같은 에러가 뜬다.

sudo: source: command not found

해법)

- 아래와 같이 루트로 로그인하여 source로 쉘을 실행한다.

sudo -s # 루트로 로그인 한다.

참조)

https://askubuntu.com/questions/20953/sudo-source-command-not-found

 

sudo: source: command not found

I've been updating some of the default profile for bash, and saw from the tutorials I was following that I could reload the new profile with the new environment settings by using: source /etc/bash.

askubuntu.com

 


2) Source로 쉘 스크립트 실행하는 것과 쉘을 직접 실행하는 것의 차이는 무엇인가?

> ./myscript

> source myscript

A. 

쉘을 실행(Execute) 할 때는 새로운 쉘을 실행하고 새로운 쉘에 명령어를 타이핑하고 결과를 현재 쉘에 복사해 온다. 그리고 새로운 쉘을 닫는다. 어떠한 환경 변화(?)도 새로운 쉘만 영향을 받고, 새로운 쉘이 닫히면 변화는 잃어 버린다.

하지만, source 명령으로 실행하게 되면 새로운 쉘을 생성하지 않고 현재 쉘에 타이핑을 하는 것이다. 따라서, 환경 변화는 현재 쉘이 영향을 받는다. 

 

환경 변화는 '환경 변수 설정', '쉘 설정' 등을 뜻한다. 

 

 

linux - What is the difference between executing a Bash script vs sourcing it? - Super User

 

What is the difference between executing a Bash script vs sourcing it?

What is the difference between executing a Bash script like A and sourcing a Bash script like B? A > ./myscript B > source myscript

superuser.com

 

반응형

'운영체제 > 리눅스(Linux)' 카테고리의 다른 글

tee 명령어  (0) 2022.09.06
쉘 프로그래밍(Shell Programming)  (0) 2022.01.13
Makefile 정리  (0) 2021.10.25