12

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.

closed as too broad by Michael Homer, Julie Pelletier, Rui F Ribeiro, phk, Dmitry Grigoryev May 19 '17 at 11:16

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

12

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
14

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.

  • Thanks very much. I think I should make the RHEL/Fedora answer the accepted one, since I did ask about that. But mainly I use debian, so yours is probably the preferred one from my point of view! A shame I can't accept both. – John Lawrence Aspden May 22 '17 at 16:59
  • The answer is correct, but additionally one might often want to look at the upstream source (e.g. whether they have new changes not yet in the distro). To do that call dpkg -s coreutils (or whatever package you are interested in) and look for the Homepage:. From the homepage you can typically find read access to their source code and full version history. – Uwe Geuder Apr 13 '18 at 18:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.