5

While learning C, I've recognized that you can see the manual of its functions within linux shell (I've been using BASH). For example:

man strlen
man crypt
man printf

I'd figured that maybe I could use those functions in shell scripting.

Is this true? How can I use those functions in shell script?

Raynal Gobel
  • 991
  • 15
  • 25
  • 1
    `man` has more than just shell command man pages; the fact that something has a man page doesn't mean a shell will understand it. – user2357112 May 22 '15 at 02:47
  • I see.. well, I think the only way to use it is to compile your own C program. Because I've been relying on `man` pages these days it give me the idea that everything that has `man` pages is executeable on shell. I guess this is not the case then. Thank you for your answers. – Raynal Gobel May 22 '15 at 02:50

2 Answers2

3

You can't. Manpages are a relic of a time when there were no IDEs, and no Web to look things up in. You would write your code in an editor such as ed or vim or emacs, look up functions with man, compile with cc. The fact that man command looks up C functions does not mean you get to use those functions directly in a shell.

However, some of those functions also have an equivalent in *NIX: man 3 printf is a C function, but man 1 printf is a *NIX one.

Amadan
  • 191,408
  • 23
  • 240
  • 301
  • Yep. This would suffice. Thank you for your knowledge. – Raynal Gobel May 22 '15 at 02:53
  • And the basic for of `ln` exercises the `link()` system call, and the `ln -s` form exercises the `symlink()` system call, and the basic form of `mkdir` exercises the `mkdir()` system call, and the basic form of `rmdir` exercises the `rmdir()` system call (though in the case of `mkdir` and `rmdir`, the system calls are a lot newer than the commands). And so on. Many of the commands are simple covers for system calls, at least in their basic incarnation. And with Bash, you can get the length of a string with `${#var}`, for example. – Jonathan Leffler May 22 '15 at 03:07
2

The short answer is, you can't use functions from the C library directly in the shell.

Look at the different man pages you get with the following commands:

man 1 printf
man 3 printf

The first one comes from section 1 (user commands), and the second one comes from section 3 (C library). While they serve a similar purpose, they are not the same. You can use the printf described in section 1 directly in the shell. Check out man 7 man to see a list of different sections.

Markku K.
  • 3,840
  • 19
  • 20
  • Got it. And the `sections` part on `man 7 man` redirect me to `man 7 man-pages`. I'll put that here as a reminder. Thank you for your time and effort. – Raynal Gobel May 22 '15 at 02:58