1

When I try to execute the exec ls, my putty session is getting closed. What is the difference between ls and exec ls ?

Why do we need the exec command, and what are the uses of this command ?

perror
  • 7,071
  • 16
  • 58
  • 85
user2720323
  • 277
  • 3
  • 6
  • 11
  • Related: http://stackoverflow.com/a/18351547/1983854 – fedorqui Sep 12 '13 at 08:29
  • possible duplicate of [What is the use of exec command in the shell scripting?](http://stackoverflow.com/questions/18351198/what-is-the-use-of-exec-command-in-the-shell-scripting) – Johan Sep 12 '13 at 09:38

2 Answers2

3

exec replaces the current process (the shell) with the new process. If you call a program without exec, the shell will fork a new process and then replace the new process with the program.

knittl
  • 246,190
  • 53
  • 318
  • 364
3

exec is a shell built-in command. In the manpage it says

If exec is specified with command, it shall replace the shell with command without creating a new process.

So when you execute exec ls in the shell, your shell will be replaced with the ls process; when this process ends, the shell exits. Compared with source or ., this could be useful in shell scripts.

rmunn
  • 34,942
  • 10
  • 74
  • 105
Marlow.W
  • 53
  • 8