How do I look at the source code for a command?

I suddenly decided I’d like to look at the source code for ‘echo’

$ which echo
/usr/bin/echo

so

$ ls -al /usr/bin/echo
-rwxr-xr-x. 1 root root 32536 Oct 31  2016 /usr/bin/echo

so

$strings /usr/bin/echo

leads me to believe it’s a compiled C program

Now I’m stuck.

How do I:

  1. Find out which package it’s in
  2. Get the source
  3. Rebuild it
  4. Test it
  5. Install the new version system-wide

(I know that 5’s not a good idea, I’m just curious…)

I’m currently on Fedora, but I’d also be interested in the answers for Debian

A link to a relevant tutorial would be a good answer.


Edit:

$ type -a echo
echo is a shell builtin
echo is /usr/bin/echo

So I guess it’s the one in /usr/bin/echo I’d like to see rather than trying to read the whole of bash.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Debian (and derivatives)

Run dpkg -S /path to see which package /path belongs to:

$ dpkg -S /bin/echo
coreutils: /bin/echo

Get the source (I’m on Ubuntu):

$ apt-get source coreutils
Reading package lists... Done
Need to get 5,755 kB of source archives.
Get:1 http://jp.archive.ubuntu.com/ubuntu xenial/main coreutils 8.25-2ubuntu2 (dsc) [2,071 B]
Get:2 http://jp.archive.ubuntu.com/ubuntu xenial/main coreutils 8.25-2ubuntu2 (tar) [5,725 kB]
Get:3 http://jp.archive.ubuntu.com/ubuntu xenial/main coreutils 8.25-2ubuntu2 (diff) [28.0 kB]
Fetched 5,755 kB in 3s (1,758 kB/s)
gpgv: Signature made 2016年02月18日 22時28分36秒 JST using RSA key ID 73F3F233
gpgv: Can't check signature: public key not found
dpkg-source: warning: failed to verify signature on ./coreutils_8.25-2ubuntu2.dsc
dpkg-source: info: extracting coreutils in coreutils-8.25
dpkg-source: info: unpacking coreutils_8.25.orig.tar.xz
dpkg-source: info: unpacking coreutils_8.25-2ubuntu2.debian.tar.xz
dpkg-source: info: applying no_ls_quoting.patch
dpkg-source: info: applying 61_whoips.patch
dpkg-source: info: applying 63_dd-appenderrors.patch
dpkg-source: info: applying 72_id_checkngroups.patch
dpkg-source: info: applying 80_fedora_sysinfo.patch
dpkg-source: info: applying 85_timer_settime.patch
dpkg-source: info: applying 99_kfbsd_fstat_patch.patch
dpkg-source: info: applying 99_hppa_longlong.patch
dpkg-source: info: applying 99_float_endian_detection.patch

Find the file:

$ cd coreutils-8.5 
$ find src -iname '*echo*'
src/echo.c

The rest is up to you.

Method 2

RHEL/Fedora

Run rpm -qf /path

$ rpm -qf /usr/bin/echo
coreutils-8.25-17.fc25.x86_64

Download the source package (use yum for RHEL):

$ dnf download coreutils --enablerepo="*source"

Extract the sources, patches from the SRPM package downloaded in current directory, change to the directory where the files are extracted and find your file:

$ rpmbuild -rp coreutils-8.25-17.fc25.src.rpm
$ cd ~/rpmbuild/BUILD/coreutils-8.25/
$ find src -iname '*echo*'
src/echo.c

You can rebuild the package using rpmbuild --rebuild coreutils-8.25-17.fc25.src.rpm, which will produce the RPMs that you can directly install on your system.

If you need to do some modification to fedora packages, it is much easier to go the maintainer way: Install fedpkg, clone the repository, do the modifications (using patches) and rebuild the package with modifications:

$ sudo dnf install fedpkg
$ fedpkg clone coreutils
$ cd coreutils
$ # do the modifications
$ fedpkg local


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x